Whenever it prompts the user to input a variable, the first entered variable does not get checked, but all variables afterwards get checked.
Here's my code:
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("^[!@#$%^&*].*")) {
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();
}
}