Validating input with Java

I am noob, so I apologize if I ask a stupid question. I ask the user to enter a numerical value. If the value is less than 12 or not a numerical value, such as a number, I want it to request them for another input. When a value of at least 12 is entered, I want this value to be assigned to a variable with a name creditUnits;

When I ask for the initial prompt, my program will catch it, if the entered value is not numeric, it will ask the user to enter "Valid number:". The while loop seems to work well.

I had a problem with the second cycle, where it is assumed that any number entered in it will be less than 12. It will ask the user to enter a value greater than 11. The problem I am facing is at this moment if the user enters any value at this moment, the program is just sitting there. Any help would be appreciated, and I apologize in advance for the carelessness of my code:

System.out.print("Enter the credits you will take each term: ");

while (!in.hasNextDouble()){
    System.out.print("Enter a valid number: ");
    in.next();
}

creditUnits = in.nextDouble();

if (creditUnits < 12){
    System.out.print("Enter a number greater than 11: ");
    in.next();
}

creditUnits = in.nextDouble();                       
System.out.println("You will be taking " + creditUnits + " credits per term.");    
+3
source share
2 answers

This is because you ask the scanner to capture the next two inputs when you only need the first.

System.out.print("Enter the credits you will take each term: ");

while (!in.hasNextDouble()){
    System.out.print("Enter a valid number: ");
    in.next();
}

creditUnits = in.nextDouble();

if (creditUnits < 12){
        System.out.print("Enter a number greater than 11: ");
        creditUnits = in.nextDouble();  
}


System.out.println("You will be taking " + creditUnits + " credits per term.")

In addition, you should consider putting a block if(creditUnits < 12)in a while loop, so you can constantly check if they have entered a number greater than 12.

Sort of:

System.out.print("Enter the credits you will take each term: ");
while (true){
    System.out.print("Enter a valid number: ");
    creditUnits = in.nextDouble();
    if (creditUnits < 12){
        System.out.print("\nNumber must be greater than 12!\n"); 
    }else
        break;
}

System.out.println("You will be taking " + creditUnits + " credits per term.");

Also, there is no such thing as a stupid question. Only stupid fans. / Joke

+4

Embattled Swag,

if (creditUnits < 12){
    System.out.print("Enter a number greater than 11: ");
    in.next();
}

in.next() , . , , double:

creditUnits = in.nextDouble();

- ( , ), " ...", . InputMismatchException.

, , , nextDouble

public static void main(String[] args) throws IOException {
    System.out.print("Enter the credits you will take each term: ");

    Scanner in = new Scanner(System.in);
    String input = in.next();

    double credits = 0.0;

    while (true) {
        try {
            credits = Double.parseDouble(input);
            if (credits < 12.0) {
                throw new IllegalArgumentException("Must take at least 12 credits.");
            } else {
                break;
            }
        } catch(IllegalArgumentException e) {
            System.out.print("Enter a number greater than 11: ");
            input = in.next();
        }
    }

    System.out.println("You will be taking " + credits + " credits per term.");
    in.close();
}
0

All Articles