2010-02-26 195 views
2

嘿!我正在嘗試做一些數據輸入驗證,但我一直無法弄清楚。當我嘗試驗證輸入的第一個字符是否是字母時,我得到一個無限循環。 。 。 。無限循環在Java中

感謝您的幫助!

public class methods 
{ 
    public static void main(String args[]) throws IOException 
    { 
     String input =""; 
     int qoh=0; 
     boolean error=true; 

     Scanner keyboard = new Scanner (System.in); 

     //while (error) 
     //{ 
      //error=true; 

     while (error==true) 
     { 
      System.out.print("\nEnter Quantity on Hand: "); 
      input = keyboard.nextLine(); 

      if (input.length() <1) 
      { 
       System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500"); 
       error=true; 
       System.out.println(qoh); 
       System.out.println(input); 
      } 
      else 
      { 
       error=false; 
      } 
     } 

     error = true; 

     while (error==true) 
     { 
      if (Character.isLetter(input.charAt(0))) 
      { 
       System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500"); 
       error=true; 
       System.out.println(qoh); 
       System.out.println(input); 
      } 
      else 
      { 
       qoh = Integer.parseInt(input); 
       error=false; 
       } 
      } 
     } 
    } 
+2

備註:while(error == true)可以寫爲while(error) – basszero 2010-02-26 15:43:20

+0

這是一項家庭作業嗎? – 2010-02-26 16:16:31

回答

3

您的第二個while循環中沒有input = keyboard.nextLine();

您可以重構您的代碼,以便在發生錯誤時僅詢問新輸入。所以,在'錯誤...'的系統出現之後,我會實際做到這個不同。開頭的'error = true'有點令人困惑,因爲可能沒有錯誤。

例如,您可以編寫一個名爲tryProcessLine方法,它讀取輸入,如果有錯誤,則返回true,如果確定與假,並不僅僅是做類似下面while(!tryProcessLine()){ }

工作例如:

import java.io.IOException; 
import java.util.Scanner; 

public class Methods { 

    private static int qoh; 

    public static void main(String args[]) throws IOException { 

    while (!tryProcessLine()) { 
     System.out.println("error... Trying again"); 
    } 

    System.out.println("succeeded! Result: " + qoh); 

    } 

    public static boolean tryProcessLine() { 

    String input = ""; 

    Scanner keyboard = new Scanner(System.in); 

    System.out.print("\nEnter Quantity on Hand: "); 

    input = keyboard.nextLine(); 

    try { 
     qoh = Integer.valueOf(input); 

     if (qoh < 0 || qoh > 500) { 
      System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500"); 
      return false; 
     } else { 
      return true; 
     } 
    } catch (NumberFormatException e) { 
     System.out.println("\n**ERROR06** - Quantity on hand must be numeric"); 
     return false; 
    } 
    } 
} 
+0

-1:這不是無限循環的原因。第一個循環被設計爲讀取第一個非空行,而我認爲第二個循環應該檢查這行只包含數字字符(而不是讀取另一行輸入)。 – Adamski 2010-02-26 15:31:55

+0

我想他想在第二個循環發生錯誤時得到一個新的輸入。否則第二個循環不應該是一個循環。因爲現在當第二個循環出現'錯誤'時,不需要新的輸入,並且'error == true'將始終爲真... – Fortega 2010-02-26 15:33:58

+0

我的猜測是他想要讀取第一個非空行輸入並嘗試將其解析爲整數。我不認爲OP需要兩個循環。 – Adamski 2010-02-26 15:45:34

1

發生死循環是因爲第二個while循環是反覆檢查字符串中的第一個字符(input.charAt(0))是否是字母。假設這個檢查的結果是真的,循環將永遠不會終止。

您的代碼可以簡化爲這樣的:

Integer qty = null; 

while (scanner.hasNext() && qty == null) { 
    String line = scanner.next(); 
    try { 
    qty = Integer.parseInt(line); 
    } catch(NumberFormatException ex) { 
    System.err.println("Warning: Ignored non-integer value: " + line); 
    } 
} 

if (qty == null) { 
    System.err.println("Warning: No quantity specified."); 
} 
0

如果它是一個字符,你允許誤差仍然=真,這是造成該循環永遠繼續下去,你永遠不回到開始並閱讀另一行。

下面是一些代碼,它可以實現你想要的,並且結構更好一些。

public class ScanInfo { 

    Scanner keyboard = new Scanner(System.in); 

    public ScanInfo(){ 
    String line = getLineFromConsole(); 
    while(null != line && !"quit".equals(line)){ 
     if(isValidInput(line)){ 
     int validNumber = Integer.parseInt(line); 
     System.out.println("I recieved valid input: "+validNumber); 
     }else{ 
     System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500"); 
     } 
     line = getLineFromConsole(); 
    } 

    } 

    private boolean isValidInput(String line){ 
    //basic sanity 
    if(null == line || line.length() < 1){ 
     return false; 
    } 


    try { 
     int number = Integer.parseInt(line); 

     return (number >= 0 && number <= 500); 

    } catch (NumberFormatException e) { 
     return false; 
    } 

    } 


    public static void main(String[] args) { 
    new ScanInfo(); 

    } 

    public String getLineFromConsole(){ 
    System.out.print("\nEnter Quantity on Hand: "); 
    return keyboard.nextLine(); 

    } 

} 
+0

爲什麼要將輸入解析爲一個整數兩次? – Adamski 2010-02-26 15:43:46

+0

「quit」!=行應該是!「quit」.equals(line) – Fortega 2010-02-26 15:53:20

+0

我解析整數兩次,因爲我懶惰,封裝它錯了。 Fortega:做出了改變。 – Kylar 2010-03-02 22:06:58

1

問題是,在本節:

     while (error==true) 
         { 
          if (Character.isLetter(input.charAt(0))) 
          { 
           System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500"); 
           error=true; 
           System.out.println(qoh); 
           System.out.println(input); 
          } 
          else 
          { 
           qoh = Integer.parseInt(input); 
           error=false; 
          } 
         } 

一旦你在第一位置的字母,這個循環可以永遠終止。它檢查一封信是否在第一個位置(它是),打印它並重復。嘗試更改爲:

      while (error==true) 
          { 
           if (Character.isLetter(input.charAt(0))) 
           { 
            System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500"); 
            error=false; 

            ... 

此外,一對夫婦的其他東西:

while (error == true)可縮短至while(error)

另外,Integer.parseInt將拋出NumberFormatException如果輸入不是一個整數 - 你需要捕捉並處理這個。

另外,爲什麼你需要第二個循環呢?它似乎只是爲了驗證輸入 - 如果是的話,你可以將這個邏輯移入第一個循環,並消除第二個循環。只對需要重複發生的事情使用循環(如用戶輸入輸入數據)。不需要重複檢查相同的輸入。