Blog

Scanner Class In Java

Java / Java Tutorials

Scanner Class In Java

Accepting user input using Scanner Class In Java

Introducing “System.in”

Before we can understand the use of the Scanner class in java, we first need to understand how java interacts by default with the keyboard. In java, just as we have an object reference called “System.out” that is used to write to the
console window, we also have another object called “System.in” to read keyboard input. So we can say that “System.in” represents InputStream i.e. java keyboard.
The object reference System.in has a method called read( ), which can read keyboard input.
The declaration of read ( ) is as below:
public int read( )

Scanner Class In Java

But it has two problems
It can only accept single character/digit/symbol
It converts every input value it has read to character

class Demo
{
public static void main(String [ ] args)throws Exception
{
char gender;
System.out.println(“Enter your gender (M/F):”);
gender=(char)System.in.read();
System.out.println(“Your gender is:”+gender);
}
}

Output:

C:\Program Files\Java\jdk1.8.0_66\bin>java Demo
Enter your gender (M/F):
M
Your gender is:M
C:\Program Files\Java\jdk1.8.0_66\bin>
class Demo
{
public static void main(String [ ] args)throws Exception
{
int age;
System.out.println(“Enter your age:”);
age=System.in.read();
System.out.println(“Your age is:”+age);
}
}

Output:

C:\Program Files\Java\jdk1.8.0_66\bin>javac Demo.java
C:\Program Files\Java\jdk1.8.0_66\bin>java Demo
Enter your age:
25
Your age is:50
C:\Program Files\Java\jdk1.8.0_66\bin>

From the previous examples we can conclude that using read( ) method is useful only when we want to accept a single character as input .
For all other inputs we have to use Scanner class

Scanner Class

Scanner class belongs to the package java.util i.e. utility. To use this class we have to import it by writing
import java.util.Scanner;
It has various methods using which we can accept inputs of all primitive data type, except character type.
Scanner class can be used to scan data not only from keyboard but also from various other input resources like files, network socket, web servers etc
To use Scanner class we will first have to connect it with an input device like keyboard .
This is done using the following statement:
Scanner kb=new Scanner(System.in);

Scanner Class In Java

Important Methods of Scanner class to accept input of primitive data types
int nextInt( )
short nextShort( )
long nextLong( )
float nextFloat( )
byte nextByte( )
double nextDouble( )
boolean nextBoolean( )
For strings
String next( )
String nextLine( )

Accepting Integer

import java.util.Scanner;
 class InputDemo
 {
 public static void main(String [ ] args)
 {
 Scanner kb=new Scanner(System.in);
 int age;
 System.out.println( “Enter your age ”);
 age=kb.nextInt( );
 System.out.println(“Your age is ”+age);
 }
}

Output

C:\Program Files\Java\jdk1.8.0_66\bin>javac InputDemo.java
C:\Program Files\Java\jdk1.8.0_66\bin>java InputDemo
Enter your age
26
Your age is 26
C:\Program Files\Java\jdk1.8.0_66\bin>

Accepting String

The next( ) method can accept string inputs without any spaces in between i.e. it can only accept single word as input
Whereas, the nextLine( ) method can accept strings with spaces in between i.e. it can accept a complete line of input

import java.util.Scanner;
class InputDemo
{
	public static void main(String [ ] args)
	{
		Scanner kb=new Scanner(System.in);
		String name;
		System.out.println("Enter  your full name");
		name=kb.nextLine( );
		System.out.println("Your name is "+name+"\nEnter your name again");
		name=kb.next( );
		System.out.println("Your name is "+name);
	}
}

Output:

C:\Program Files\Java\jdk1.8.0_66\bin>javac InputDemo.java
C:\Program Files\Java\jdk1.8.0_66\bin>java InputDemo
Enter  your full name
John Martin
Your name is John Martin
Enter your name again
John Martin
Your name is John
C:\Program Files\Java\jdk1.8.0_66\bin>

InputMismatchException

When Scanner is given wrong input it gives an exception. i.e. when the data type and method called does not match.
The exception is named as InputMismatchException.
Sample output :-

C:\Program Files\Java\jdk1.8.0_66\bin>javac InputDemo.java
C:\Program Files\Java\jdk1.8.0_66\bin>java InputDemo
Enter your age
25.9
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 InputDemo.main(InputDemo.java:9)
C:\Program Files\Java\jdk1.8.0_66\bin>

Checkout the below program to add 2 no.s given by user in java
Program two add 2 no.s given by user