2011-04-04 57 views
4

我一直在爲學校創建一門課程,用戶,學生和老師,但我在創建課程時碰到了一個障礙。在java中讀取字符串

我想要做的是(在終端中)讀取輸入流,例如「CSC 110 Into to java」,並將其全部輸入到字符串中。我正在使用掃描儀進行輸入。並在next()之後嘗試了hasNext()和nextLine()。我想寫一個函數來使用char數組獲取整個字符串,然後將char數組轉換爲字符串。

首先我想知道是否有更簡單的解決方案?

編輯:對不起,我不清楚。沒有進入我的所有代碼。

當前代碼:

 System.out.print("Enter the Course Title: "); 
     title = keyboard.nextLine(); 
     System.out.print("Enter a Brief Course Description: "); 
     desc = keyboard.nextLine(); 

     if (t != null) 
     { 
      do 
      { 
       System.out.println("\nPick a teacher\n"); 

       for (int j = 0; j < t.size(); j++) 
       { 
        nTeacher = t.get(j); 
        s += "\t(" + (j+1) + ") " + nTeacher + "\n"; 
       } 

       System.out.print(s + "\nEnter the Teacher ID: "); 
       int tId = keyboard.nextInt(); // My error 

當前出錯:

Enter the Course Title: CSC 110 Into to java 

Enter a Breif Course Description: 
Pick a teacher 

Enter the Teacher ID: 
Exception in thread "main" java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Scanner.java:857) 
    at java.util.Scanner.next(Scanner.java:1478) 
    at java.util.Scanner.nextInt(Scanner.java:2108) 
    at java.util.Scanner.nextInt(Scanner.java:2067) 
    at Prototype.classCreator(Prototype.java:530) 
    at Prototype.mainMenu(Prototype.java:181) 
    at Prototype.login(Prototype.java:121) 
    at Prototype.main(Prototype.java:48) 

當我使用nextLine()

Create Class 
Enter the courseID: 27222 
Enter the Course Title: Enter a Brief Course Description: 

預期的結果:

Create Class 
Enter the courseID: 27299 
Enter the Course Title: CSC 110 Into to Java 
Enter a Brief Course Description: Introduction to programming 
Pick a teacher 
(1) ID: 1111 Name: Bob 
Enter the Teacher ID: 1 
Class Created 

我知道,當我有三個字符串「CSC 110 Intro」它得到nextInt()的輸入,但我不明白爲什麼nextLine()正在寫我的println是這樣的。

+0

對不起新的SO,無形中。我修正了:) – Jeremy 2011-04-04 06:56:39

回答

5

好了,你還沒告訴我們的錯誤是什麼,所以我就給出一些示例代碼掃描儀,讓您的工作:

//create the Scanner 
Scanner terminalInput = new Scanner(System.in); 

//read input 
String s = terminalInput.nextLine(); 

應工作;掃描儀是最簡單的類之一。也許你不小心用

new Scanner(); //no input source! 

而不是

new Scanner(System.in); //links to terminal 

編輯:

我從你的輸出推測,您所呼叫

keyboard.nextInt(); 

後來

keyboard.nextLine(); 

這就是搞亂你的程序。 nextInt()方法留下「\ n」結束符號,並由nextLine()立即拾取,跳過下一個輸入。你想要做的是使用nextLine的一切,後來又對其進行分析:

String nextIntString = keyboard.nextLine(); //get the number as a single line 
int nextInt = Integer.parseInt(nextIntString); //convert the string to an int 

這是迄今爲止最簡單的方法,以避免出現問題 - 不要混合你的「下一個」的方法。只使用nextLine(),然後解析整數/單詞。

+0

很好的解決方案:) – Shankar 2011-04-04 05:10:46

+0

你能檢查我的編輯。我試過nextLine(),它遇到了一個問題。 – Jeremy 2011-04-04 06:53:01

+0

您的編輯未能提供您在使用nextLine()問題時使用的代碼... – donnyton 2011-04-04 12:19:30

4
Scanner scan = new Scanner(System.in); 
String str_input = scan.nextLine(); 

這樣可以嗎?

+0

如果這個答案適合你,你應該接受它。 – 2011-04-04 05:17:41

+0

你可以檢查我的編輯。我試過nextLine(),它遇到了問題 – Jeremy 2011-04-04 06:53:20

+0

你在那裏輸入一個整數嗎? InputMismatchException聽起來像沒有收到整數。不過,我可能會改變它不使用nextInt(),而是在整個使用nextLine()。 nextInt()和其他允許用戶在同一行輸入很多整數(例如「25 18 76」),如果您以後期望完整行,可能會爲您的界面創建問題。雖然可修復,但使用nextLine()獲取字符串可能會更容易,然後使用Integer.parseInt()。 – 2011-04-04 15:41:24

2

可以使用new Scanner(System.in)由幾個人提到的,或者你也可以試試這個:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

String s = br.readLine(); 
0
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName)); 

這將是你將如何從文件中讀取BTW它。

我認爲BufferedReader比掃描器更好,只是因爲它按照它所說的做了緩衝。

但yea掃描儀也工作。

嘗試製作一個配置文件,而不是從命令行讀取它。

Properties configFile = new Properties(); 
configFile.load(new FileInputStream("conf.properties")); 

myString = configFile.getProperty("MY_STRING"); 

配置文件將被命名爲conf.properties,然後它會在裏面:

MY_STRING = blahblahbah