Validating input using java.util.Scanner


I'm taking user input from using a java.util.Scanner. I need to validate the input for things like:

  • It must be a non-negative number
  • It must be an alphabetical letter
  • ... etc

What's the best way to do this?

5/8/2011 9:52:32 AM

Here's a minimalist way to do it.

System.out.print("Please enter an integer: ");
int demoInt = scan.nextInt();

For checking Strings for letters you can use regular expressions for example:


For checking numbers and stopping the program crashing, I have a quite simple class you can find below where you can define the range of values you want. Here

    public int readInt(String prompt, int min, int max)
    Scanner scan = new Scanner(;

    int number = 0;

    //Run once and loop until the input is within the specified range.
        //Print users message.
        System.out.printf("\n%s > ", prompt);

        //Prevent string input crashing the program.
        while (!scan.hasNextInt()) 
            System.out.printf("Input doesn't match specifications. Try again.");
            System.out.printf("\n%s > ", prompt);

        //Set the number.
        number = scan.nextInt();

        //If the number is outside range print an error message.
        if (number < min || number > max)
            System.out.printf("Input doesn't match specifications. Try again.");

    } while (number < min || number > max);

    return number;

One idea:

try {
    int i = Integer.parseInt(myString);
    if (i < 0) {
        // Error, negative input
} catch (NumberFormatException e) {
    // Error, not a number.

There is also, in commons-lang library the CharUtils class that provides the methods isAsciiNumeric() to check that a character is a number, and isAsciiAlpha() to check that the character is a letter...


If you are parsing string data from the console or similar, the best way is to use regular expressions. Read more on that here:

Otherwise, to parse an int from a string, try Integer.parseInt(string). If the string is not a number, you will get an exception. Otherise you can then perform your checks on that value to make sure it is not negative.

String input;
int number;
    number = Integer.parseInt(input);
    if(number > 0)
        System.out.println("You positive number is " + number);
} catch (NumberFormatException ex)
     System.out.println("That is not a positive number!");

To get a character-only string, you would probably be better of looping over each character checking for digits, using for instance Character.isLetter(char).

String input
for(int i = 0; i<input.length(); i++)
      System.out.println("This string does not contain only letters!");

Good luck!


what i have tried is that first i took the integer input and checked that whether its is negative or not if its negative then again take the input

Scanner s=new Scanner(;

    int a=s.nextInt();
    System.out.println("please provide non negative integer input ");
    System.out.println("the non negative integer input is "+a);

Here, you need to take the character input first and check whether user gave character or not if not than again take the character input

    char ch = s.findInLine(".").charAt(0);
    System.out.println("please provide a character input ");
    System.out.println("the character  input is "+ch);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]