0

As the title says, i'm attempting to make my program re-ask for user input if the given input is invalid (In this case, invalid input is any input that is not an integer)

I've already tried this, but it does not work:


import java.util.Scanner;

public class Main {



    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Please input your age");

        if (scanner.hasNextInt()) {
            int age = scanner.nextInt();
            System.out.println("Your age is: " + age);
            System.out.println();
        } else {
            System.out.println("This input is not an integer - Please try again!");
            int age = scanner.nextInt();
            System.out.println("Your age is: " + age);
            System.out.println();

        }
    }
}

I'm aiming for this to be done with while loop and scanner

My current code:


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {



        Scanner scanner = new Scanner(System.in);
        System.out.println("Please input your age");


        if (scanner.hasNextInt()) {
            int age = scanner.nextInt();
            System.out.println("Your age is: " + age);

        }
    }
}

Any reply on this post is greatly appreciated.

3
  • I'm aiming for this to be done with while loop and scanner. You have not used while loop anywhere in your program. Commented May 28, 2021 at 15:41
  • I have not used while loop anywhere because i do not know how to approach it in the code. I know how while loop works in general, but in this case i do not see how it can be done with re-asking for input with scanner. Commented May 28, 2021 at 15:45
  • 1
    See the accepted answer of the linked question and there "Example 1: Validating positive ints". It shows you how to validate the input for numbers and when you don't want to validate for positive ints, then remove the outer while loop and keep the inner one. Commented May 28, 2021 at 15:51

3 Answers 3

4

You can use while-loop:

Scanner scanner = new Scanner(System.in);
boolean ageGiven = false;

while (!ageGiven) {
    System.out.println("Please input your age");
    String next = scanner.next();
    try {
        int age = Integer.parseInt(next);
        System.out.println("Your age is: " + age);
        ageGiven = true;
    } catch (NumberFormatException e) {
        System.out.println("This input is not an integer - Please try again!");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is exactly what i was searching for. It makes sense now.
2

I think you should put while instead of if statement. Break after having correct input. Try out once.

Comments

0

As you suggested, a while loop is more appropriate to this. Something like this should work:

Scanner scanner = new Scanner(System.in);
System.out.println("Please input your age");

boolean isValid = false;
int age;

while (!isValid) {
    if (scanner.hasNextInt()) {
        age = scanner.nextInt();
        isValid = true;
    }
    else {
        System.out.println("Invalid input. Please type a number");
    }
}

System.out.println("Your age is: " + age);

In this case, it would loop while the IsValid boolean is false. And that variable would be set to true, once the user inputs a valid age.

Edit: Changed the code to check if the input is an integer.

2 Comments

Thank you for your answer, this does not solve my problem though, the program should re-ask for input if the given input is NOT an integer. For example, if i enter "hi" the program should print an error message and ask for input again.
Ah i see. I edited the answer to reflect that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.