-1
public static void main(String[] args) {

    {
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter your name: ");
        String n = reader.nextLine();
        System.out.println("You chose: " + n);
    }

    {
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter your age: ");
        int n = reader.nextInt();
        System.out.println("You chose: " + n);
    }

    {
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter your email: ");
        String n = reader.nextLine();
        System.out.println("You chose: " + n);
    }
}

If a user places anything else under Enter your age other than a number, how do I make it say that the input is not correct and ask again?

RamenChef
  • 5,533
  • 11
  • 28
  • 39
Johnny
  • 1
  • 1
  • 2
  • You need a `while` loop to verify that the correct input was put in first. and keep prompting for input until you get the number. Look up some tutorials on `while` loops if you are unfamiliar with them. – Orin Sep 06 '16 at 18:06
  • Also, please note that `nextInt()` does not consume end of line character(s). See [this post](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) for more info. – PM 77-1 Sep 06 '16 at 18:08

2 Answers2

2

You can get the line provided by the user, then parse it using Integer.parseInt(String) in a do/while loop as next:

Scanner reader = new Scanner(System.in);
Integer i = null;
// Loop as long as i is null 
do {
    System.out.println("Enter your age: ");
    // Get the input from the user
    String n = reader.nextLine();
    try {
        // Parse the input if it is successful, it will set a non null value to i
        i = Integer.parseInt(n);
    } catch (NumberFormatException e) {
        // The input value was not an integer so i remains null
        System.out.println("That's not a number!");
    }
} while (i == null);
System.out.println("You chose: " + i);

A better approach that avoids catching an Exception based on https://stackoverflow.com/a/3059367/1997376.

Scanner reader = new Scanner(System.in);
System.out.println("Enter your age: ");
// Iterate as long as the provided token is not a number
while (!reader.hasNextInt()) {
    System.out.println("That's not a number!");
    reader.next();
    System.out.println("Enter your age: ");
}
// Here we know that the token is a number so we can read it without
// taking the risk to get a InputMismatchException
int i = reader.nextInt();
System.out.println("You chose: " + i);
Community
  • 1
  • 1
Nicolas Filotto
  • 39,066
  • 11
  • 82
  • 105
0
  1. No need to declare a variable scanner so often, simply once
  2. care with nextLine(); for strings; presents problems with blanks, advise a .next();

use do-while

do
  {
    //input
  }
  while(condition);//if it is true the condition returns to do otherwise  leaves the cycle

use blocks try{ .. }catch(Exception){..}

to catch exceptions mismatch-input-type exception is when the input is not what I expected in the example enter a letter when a number expected

        Scanner reader = new Scanner(System.in);
        int n=0;
        do
        {
              System.out.println("Enter your age: ");
          try {
               n = reader.nextInt();
           } 
         catch (InputMismatchException e) {
             System.out.print("ERROR NOT NUMBER"); 
          }

        }
        while(n<0 && n>100);//in this case if the entered value is less than 0 or greater than 100 returns to do


        System.out.println("You chose: " + n);
Dev. Joel
  • 1,074
  • 1
  • 8
  • 13