2015-11-17 4 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

Возможный дубликат [Пропуск nextLine() после использования следующих(), nextInt() или других методов nextFoo()] (http://stackoverflow.com/questions/13102045/skipping-nextline-after -Использование-nextint следующего или-другой-nextfoo-метода) – Raf

ответ

0

Это происходит потому, что у вас есть линия

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(); 

    } 

} 
Смежные вопросы