The Scanner class from java.util package is used to get input of primitive types such as int, double, and strings. It’s the simplest way to read input in a Java program, but it’s inefficient if you need an input method for situations where time is a factor, such as competitive programming.
Table of Contents
What is the Scanner Class in Java with Example?
The Scanner class, which is part of java.util package is mainly used to obtain user input. You can utilize the Scanner class by creating a class object and calling any Scanner class methods.
We usually pass the preconfigured object System.in, which represents the standard input stream, when creating an instance of the Scanner class. If we want to read data from a file, we can pass an object of the File class.
Examples
The function nextXYZ() is used to read numerical values of the data type XYZ. To read a value of type short, we can use nextShort(). We can use nextLine() to read strings.
We can use next().charAt(0) to read a single character. The next() method returns a string representing the input’s next token/word, whereas the charAt(0) method returns the first character in that string.
Let’s have a look at a coding example with different data types.
Code
import java.util.Scanner;
public class Student
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name:");
String name = sc.nextLine();
System.out.println("Enter your Id:");
char id = sc.next().charAt(0);
System.out.println("Enter your age:");
int age = sc.nextInt();
System.out.println("Enter your contact:");
long contact = sc.nextLong();
System.out.println("Enter your CGPA:");
double cgpa = sc.nextDouble();
System.out.println("\nYour Data has been saved.");
System.out.println("Name: "+name);
System.out.println("Id: "+id);
System.out.println("Age: "+age);
System.out.println("Contact: "+contact);
System.out.println("CGPA: "+cgpa);
}
}
Output

What is a token java scanner?
We must check if the next data we read is of a specified type or if the input has terminated regularly (EOF marker encountered).
The hasNextXYZ() operations are then used to determine whether the scanner’s input is the desired type, where XYZ is the selected type. The procedure returns true if the scanner has a token of that type; otherwise, it returns false.
In the code below, we’ve used hasNextInt() as an example. hasNextLine() is used to check for a string. hasNext() can be used similarly. Use charAt(0) to find a single character.
Let’s have a look at its example too.
Code
import java.util.Scanner;
public class Mean
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int sum = 0, count = 0;
System.out.println("Enter numbers to find mean.");
while (sc.hasNextInt())
{
System.out.println("Enter a number:");
int num = sc.nextInt();
sum += num;
count++;
}
int mean = sum / count;
System.out.println("Mean: " + mean);
}
}
Output

Conclusion
In conclusion, we discussed scanner java. We discussed various things about it, like its introduction/definition and its examples. Use of the scanner class in java with coding examples and token java scanner with its examples.