1

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

    }

}
PM 77-1
  • 11,712
  • 18
  • 56
  • 99
TheNewYo
  • 23
  • 2
  • 1
    you are calling `theVariable = scan.nextLine();` before the while loop, and immidiatly call it again as the first statement in the loop. I´d guess that´s what your `not checked` should mean. – SomeJavaGuy Nov 17 '15 at 16:09
  • 1
    Fixed! Thank you, should delete this post – TheNewYo Nov 17 '15 at 16:10
  • 1
    Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Raf Nov 17 '15 at 16:11

2 Answers2

0

This is because you have the line

theVariable = scan.nextLine(); 

outside your while loop. Remove that line and you should be golden.

Eugen Hotaj
  • 383
  • 2
  • 10
0

You are setting the variable value to the next scanned input before you do any checking.

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("^[!@#$%^&*].*")) {
       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();

    }

 }
Joseph Morgan
  • 163
  • 1
  • 9