2015-11-17 44 views
1

只要它提示用戶輸入變量,第一個輸入的變量就不會被檢查,但所有變量都會被檢查。驗證項目 - 程序不接受來自用戶的第一個輸入

這裏是我的代碼:

import java.util.*; 

public class classOfValidation { 

    public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    String theVariable = null; 

    System.out.println("This program checks the validity of variables"); 
    System.out.println("Please enter a variable (or press 'q' to quit)"); 
    theVariable = scan.nextLine(); 

    while (true) { 
     theVariable = scan.nextLine(); 
      if ("q".equals(theVariable)) { 
       System.out.println("Program quitted. Goodbye!"); 
       continue; 
      }  
      if (theVariable.matches("^\\d+.*|.*\\s+.*")) { 
       System.out.println("The variable is illegal"); 
       System.out.println("Please enter a variable (or press 'q' to quit)"); 
       continue; 
      }  
      if (theVariable.matches("^[[email protected]#$%^&*].*")) { 
       System.out.println("The variable is legal, but has bad style"); 
       System.out.println("Please enter a variable (or press 'q' to quit)"); 
       continue; 
      } 
     break; 
    } 

    System.out.println("The variable is legal and has good style"); 
    scan.close(); 

    } 

} 
+1

你調用'theVariable = scan.nextLine();'while循環之前,和immidiatly再次調用它作爲循環中的第一個語句。我猜這就是你的「不檢查」應該是什麼意思。 – SomeJavaGuy

+1

固定!謝謝,應該刪除這篇文章 – TheNewYo

+1

使用next(),nextInt()或其他nextFoo()方法之後跳過nextLine()的可能的副本(http://stackoverflow.com/questions/13102045/skipping-nextline-after - 使用 - 下一nextint - 或其它-nextfoo的方法) – Raf

回答

0

這是因爲你有你的while循環線外

theVariable = scan.nextLine(); 

。刪除該行,你應該是金。

0

在進行任何檢查之前,您正在將變量值設置爲下一個掃描輸入。

public static void main(String[] args) { 
Scanner scan = new Scanner(System.in); 
String theVariable = null; 

System.out.println("This program checks the validity of variables"); 
System.out.println("Please enter a variable (or press 'q' to quit)"); 

//Here the variable is the value of the first input. 

theVariable = scan.nextLine(); 
//The variable has not been checked but right after the "while (true)" you scan for the next value. 

while (true) { 
//Here you replace the first value with the second and never check it.  

theVariable = scan.nextLine(); 
//move this to the end of your lool, so the variable gets replace with the next one AFTER the checking code has run on the first one. 

     if ("q".equals(theVariable)) { 
      System.out.println("Program quitted. Goodbye!"); 
     continue; 
     }  
     if (theVariable.matches("^\\d+.*|.*\\s+.*")) { 
     System.out.println("The variable is illegal"); 
     System.out.println("Please enter a variable (or press 'q' to quit)"); 
     continue; 
     }  
     if (theVariable.matches("^[[email protected]#$%^&*].*")) { 
     System.out.println("The variable is legal, but has bad style"); 
     System.out.println("Please enter a variable (or press 'q' to quit)"); 
     continue; 
//Move it to here, scanning the next variable after the first is checked. 
    theVariable = scan.nextLine(); 
     } 
    break; 
} 

System.out.println("The variable is legal and has good style"); 
scan.close(); 

    } 

}