2015-06-22 59 views
0

我已經看過類似的問題,並試圖解決其他人遇到的問題,但把sc.next()或sc.nextLine )在while循環之後導致它進入無限循環。Java掃描儀在數據類型驗證錯誤輸入後沒有讀取newLine while循環

問題是,如果用戶在輸入正確的信息前多次輸入錯誤的輸入(無數或數字),我會得到空行。正如我的輸出塊所示,當我輸入1作爲名字時,我必須輸入它兩次,才能輸出它是錯誤的格式,然後輸入空行,直到我輸入另一個字符。

我知道它與nextInt()沒有閱讀換行符有關,但是如何讓它在此方法中正確工作?

protected static void setFirstName(){ 
    System.out.print("First Name: "); 

    while(sc.nextLine() == "" || sc.nextLine().isEmpty()){ 
     System.out.println("Name is empty. Please Try again.\nFirst name:"); 
     sc.nextLine(); 
    } 

    while (sc.hasNextInt() || sc.hasNextDouble()) { 
     System.out.print("Incorrect Name format. Please Try again.\nFirst name: "); 
     sc.nextLine(); 
     } 

    firstName = sc.nextLine();  
    firstName = Character.toUpperCase(firstName.charAt(0)) + firstName.substring(1);   
} 

這裏的輸出我得到當我第一次輸入一個號碼,然後空白的回報:

First Name: 1 
1 
Incorrect Name format. Please Try again. 
First name: 



1 
Incorrect Name format. Please Try again. 
First name: Incorrect Name format. Please Try again. 
First name: Incorrect Name format. Please Try again. 
First name: Incorrect Name format. Please Try again. 
First name: Incorrect Name format. Please Try again. 
First name: 

這裏的輸出我得到當我第一次進入一個空白的回報,然後一個數字:

First Name: 
Name is empty. Please Try again. 
First name: 
1 
1 
1 
1 
Incorrect Name format. Please Try again. 
First name: 
+0

你的問題不清楚究竟發生了什麼問題。但是,將調用與'sc.next()'(通常不會從輸入行的末尾拉動換行符)和'sc.nextLine()'調用是非常糟糕的主意, 。嘗試將你的三個調用改爲'sc.next()'到'sc.nextLine()'。 –

+0

謝謝,我按照你的建議完成了。 –

回答

4

您正在閱讀的東西太多了!

在這一行

while (sc.nextLine() == "" || sc.nextLine().isEmpty()) 

你基本上是讀取來自掃描儀的線路,它(*)與""比較,然後忘記它,因爲你再讀取下一行。因此,如果第一個讀取行確實包含名稱,則第二個查詢(isEmpty)將發生在完全其他字符串上。

結論:只讀一行,驗證它,如果它無效,只能再次閱讀。

static void setFirstName() { 
    String line; 
    boolean valid = false; 

    do { 
     System.out.print("First Name: "); 
     line = sc.nextLine(); 
     if (line.isEmpty()) { 
      System.out.println("Name is empty. Please try again."); 
     } else if (isNumeric(line)) { 
      System.out.print("Incorrect name format. Please try again."); 
     } else { 
      valid = true; 
     } 
    } while (!valid); 

    firstName = Character.toUpperCase(line.charAt(0)) + line.substring(1); 
} 

static boolean isNumeric(String line) { 
    ... 
} 

isNumeric方法有些複雜。看看其他SO問題(和答案),例如this one

(*)比較字符串必須使用equals方法完成,而不是使用==。看看here

+0

完美工作。謝謝。標記爲已解決;) –

0

嘗試這種方法

我不知道你在哪裏打開和關閉您的Scanner,所以我也把它留下了。只要確保close()它。

public static void setFirstName() { 
    System.out.print("First Name: "); 

    String firstName = sc.nextLine(); 
    while (true) { 
     try { 
      int test = Integer.parseInt(firstName); 
     } catch (Exception e) { 
      if (firstName != null && !firstName.trim().equals("")) { 
       break; // breaks loop, if input is not a number and not empty 
      } 
     } 
     System.out.println("Name is empty. Please Try again.\nFirst name:"); 
     firstName = sc.nextLine(); 
    } 

    firstName = Character.toUpperCase(firstName.charAt(0)) 
      + firstName.substring(1); 

} 
-1

讓這樣的:

public static String firstName = null; 

public static void setFirstName() { 
    firstName = null; 
    System.out.println("First Name:"); 
    Scanner sc = new Scanner(System.in); 
    while(firstName == null) { 
     String st = sc.next(); 
     try { 
      Double.parseDouble(st); 
      System.out.println("Incorrect Name format. Please Try again."); 
     } catch(Exception ex) { 
      firstName = st;  
      firstName = Character.toUpperCase(firstName.charAt(0)) + firstName.substring(1); 
     } 
    } 
    sc.close(); 
} 

如果該行不填寫,但不計。

+0

您需要檢查空字符串以避免在輸入任何內容時發生'StringIndexOutOfBoundsException'。 'firstName.substring(1)'不會在'firstName.length()'爲0的時候表現良好。 – dly

+0

我已經測試過它,它工作正常...所以我不知道我的代碼有什麼問題。它很短,它的工作原理。 –

+0

什麼都不輸入,然後按回車並檢查發生了什麼。 – dly