CyberMaestro
CyberMaestro

Reputation: 15

Validation of an int value

I'm trying to print a message saying "Wrong", followed by another chance to enter the value every time the user enters something that's not an int.

I have the following code:

do {

    System.out.println("Enter last name: ");
    lastName = input.next();

    System.out.println("Enter first name: ");
    firstName = input.next();

    do {
        System.out.println("Enter exam 1 score: ");
        if (input.hasNextInt()) {
            ex1 = input.nextInt();

            System.out.println("Enter exam 2 score: ");
            ex2 = input.nextInt();

            System.out.println("Enter exam 3 score: ");
            ex3 = input.nextInt();

            valid = true;

        } else {
            System.out.println("Incorrect choice. Write the score in numbers." + "\n");
            valid = false;
            input.next(); // Prevents infinite loop
        }
    } while (!valid);

which seems to work fine only if the user makes a mistake for exam 1, but if they do it on exam 2 or 3, it gives me an error.

I'll appreciate your help.

Upvotes: 0

Views: 122

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191701

Try not to write the same code again and again. Use a loop.

final int MAX_SCORES = 3;

while (true) {
    System.out.println("Enter last name: ");
    String lastName = input.next();

    System.out.println("Enter first name: ");
    String firstName = input.next();

    int scores = new int[MAX_SCORES];
    for (int i = 0; i < MAX_SCORES; ) {
        System.out.printf("Enter exam %d score: ", i + 1);
        if (input.hasNextInt()) { 
            scores[i++] = input.nextInt(); // increment 'i' after assignment
        } else {
             System.out.println("Incorrect choice. Write the score in numbers.\n");
             // Loop will repeat at same value of 'i'
        }
    }  

    System.out.println("Again? (Y/N)");
    if (input.next().equalsIgnoreCase("n")) break; // Prevents infinite loop
}

Upvotes: 1

dodo
dodo

Reputation: 156

You've only made the check for input user 1. You need to do seperate if statements for each user.

    if (input.hasNextInt()) {
        ex1 = input.nextInt();
    } else {
        System.out.println("Incorrect choice. Write the score in numbers." + "\n");
        valid = false;
        input.next(); // Prevents infinite loop
    }
        System.out.println("Enter exam 2 score: ");
    if (input.hasNextInt()) {
        ex2 = input.nextInt();
    } else {
        System.out.println("Incorrect choice. Write the score in numbers." + "\n");
        valid = false;
        input.next(); // Prevents infinite loop
    }
        System.out.println("Enter exam 3 score: ");
    if (input.hasNextInt()) {
        ex3 = input.nextInt();

        valid = true;

    } else {
        System.out.println("Incorrect choice. Write the score in numbers." + "\n");
        valid = false;
        input.next(); // Prevents infinite loop
    }

It would be better to wrap up the else code in a method or something, but that's the idea.

Upvotes: 0

Related Questions