Javatpoint Logo
Javatpoint Logo

Java Scanner class

There are various ways to read input from the keyboard, the java.util.Scanner class is one of them.

The Java Scanner class breaks the input into tokens using a delimiter that is whitespace bydefault. It provides many methods to read and parse various primitive values.

Java Scanner class is widely used to parse text for string and primitive types using regular expression.

Java Scanner class extends Object class and implements Iterator and Closeable interfaces.

Commonly used methods of Scanner class

There is a list of commonly used Scanner class methods:

public String next()it returns the next token from the scanner.
public String nextLine()it moves the scanner position to the next line and returns the value as a string.
public byte nextByte()it scans the next token as a byte.
public short nextShort()it scans the next token as a short value.
public int nextInt()it scans the next token as an int value.
public long nextLong()it scans the next token as a long value.
public float nextFloat()it scans the next token as a float value.
public double nextDouble()it scans the next token as a double value.

Java Scanner Example to get input from console

Let's see the simple example of the Java Scanner class which reads the int, string and double value as an input:


       Enter your rollno
       Enter your name
       Rollno:111 name:Ratan fee:450000

Java Scanner Example with delimiter

Let's see the example of Scanner class with delimiter. The \s represents whitespace.


Next TopicPrintStream class

Please Share

facebook twitter google plus pinterest tumbler

Learn Latest Tutorials