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.

Ageforce
  • 3
  • 1
  • *I'm aiming for this to be done with while loop and scanner.* You have not used `while` loop anywhere in your program. – AKSingh May 28 '21 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. – Ageforce May 28 '21 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. – Tom May 28 '21 at 15:51

3 Answers3

2

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

Most Needed Rabbit
  • 2,058
  • 1
  • 4
  • 9
2

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!");
    }
}
Most Needed Rabbit
  • 2,058
  • 1
  • 4
  • 9
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.

Jose Nuno
  • 507
  • 2
  • 8
  • 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. – Ageforce May 28 '21 at 15:54
  • Ah i see. I edited the answer to reflect that. – Jose Nuno May 28 '21 at 15:57