Java Scanner class

Java Scanner class

To make coding less, the Designers introduced java.util.Scanner (Scanner is not from package) in Java 5. Earlier to Java Scanner class, keyboard input was taken using DataInputStream and BufferedReader, both requiring extra code of parsing the user input. Java Scanner avoids parsing.
Example on Java Scanner taking Keyboard input

Java Scanner
Output screenshot on Java Scanner

nextLine(), nextInt() and nextDouble() reads, takes user input returns as a string, int and double which can be used straightaway in the coding for all arithmetic operations.

For another example where you can take name, age and salary in a single statement refer Keyboard Input – java.util.Scanner – No parsing

For more on Scanner coding refer Scanner – Checking for Tokens

One thought on “Java Scanner class

Leave a Reply

Your email address will not be published. Required fields are marked *