Read Type-safe Inputs using Scanner

Learn to read typesafe user inputs from the system console in any interactive Java application. This helps in prompting the user until the user inputs the value in the correct format/datatype.

1. Cause of InputMismatchException

Ever tried using Scanner class to read user’s input in your interactive console based program? It’s very simple code to write.

The application asks the user a question and the user inputs the value and presses ENTER. Then application reads the value using Scanner.next() methods.

Let’s see an example for reading input from console using Scanner class.

Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age as an integer : ");

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

scanner.close();

The output of the above program depends on the input user gives through the console.

Enter your age as an integer : 10
Your age is 10
Enter your age as an integer : ten
Exception in thread "main" java.util.InputMismatchException
  at java.util.Scanner.throwFor(Scanner.java:864)
  at java.util.Scanner.next(Scanner.java:1485)
  at java.util.Scanner.nextInt(Scanner.java:2117)
  at java.util.Scanner.nextInt(Scanner.java:2076)
  at com.howtodoinjava.examples.TypeSafeInputExample.typeUnsafeReadExample(TypeSafeInputExample.java:19)
  at com.howtodoinjava.examples.TypeSafeInputExample.main(TypeSafeInputExample.java:9)

As the user typed the incorrect input based on his understanding, but it crashed the application because of InputMismatchException exception.

Let’s solve this usecase.

2. Reading Typesafe Inputs

Theoretically, we can make our program more robust by checking, before we read, that the next token matches our expected input.

The Scanner.hasNext() methods do the exact thing. These methods return true if the next token can be read as the data type requested.

For example, if we are expecting int value then calling Scanner.hasNextInt() will return true only if the next available token in the scanner can be parsed as an int value. Else it will return false and we can notify the user that the value typed is not valid and re-prompt for new input.

Let’s see the above solution using code.

Scanner scanner = new Scanner(System.in);

System.out.print("Enter your age : ");

while (!scanner.hasNextInt())
{
  scanner.nextLine(); //clear the invalid input before prompting again
  System.out.print("Please enter your age in natural positive number : ");
}

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

scanner.close();
Enter your age : ten
Please enter your age in natural positive number : 10.5
Please enter your age in natural positive number : 10
Your age is 10

Using Scanner.hasNextXYZ() and Scanner.nextXYZ() methods, we can write any console-based interactive java application where we can force the user to enter valid inputs only – without crashing the program.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode