User Input/Output

 

User Input/Output

 

Taking Input:

To use the Scanner class, we need to import the java.util.Scanner package.


import java.util.Scanner;


Now the Scanner class has been imported. We create an object of the Scanner class to use the methods within it.


Scanner sc = new Scanner(System.in)


The System.in is passed as a parameter and is used to take input.


Note: Your object can be named anything, there is no specific convention to follow. But we normally name our object sc for easy use and implementation.

 

There are four ways to create an object of the scanner class. Let us see how we can create Scanner objects:

A. Reading keyboard input:

Scanner sc = new Scanner(System.in)

B. Reading String input:

Scanner sc = new Scanner(String str)

C. Reading input stream:

Scanner sc = new Scanner(InputStream input)

D. Reading File input:

Scanner sc = new Scanner(File file)

 

So how does a Scanner class work?

The scanner object reads the entered inputs and divides the string into tokens. The tokens are usually divided based upon whitespaces and newline.


Example:

Maxon
19
78.95
O


The above input will be divided as “Maxon”, “19”, “78.95” and “O”. Then the Scanner object will iterate over each token and then it will read each token depending upon the type of Scanner object created.

 

Now, we have seen how Scanner object is created, different ways to create it, how it reads input, and how it works. 


Let us see different methods to take inputs.

Below are the methods that belong to the scanner class:


Method

Description

nextLine()

Accepts string value

next()

Accept string till whitespace

nextInt()

Accepts int value

nextFloat()

Accepts float value

nextDouble()

Accepts double value

nextLong()

Accepts long value

nextShort()

Accepts short value

nextBoolean()

Accepts Boolean value

nextByte()

Accepts Byte value

 

Example:

import java.util.Scanner;

public class ScannerExample {
	public static void main(String[] args) {
		
        Scanner sc = new Scanner(System.in);
 
        System.out.println("Enter Name, RollNo, Marks, Grade");
        
        String name = sc.nextLine();		//used to read line
        int RollNo = sc.nextInt();			//used to read int
        double Marks = sc.nextDouble();		//used to read double
        char Grade = sc.next().charAt(0);	//used to read till space
 
        System.out.println("Name: "+name);
        System.out.println("Gender: "+RollNo);
        System.out.println("Marks: "+Marks);
        System.out.println("Grade: "+Grade);
        
        sc.close();
	}
}


Output:

Enter Name, RollNo, Marks, Grade
Maxon
19
87.25
A
Name: Maxon
Gender: 19
Marks: 87.25
Grade: A

 

Entering the wrong type of input, or just messing up with the order of inputs in the above example will give an error.


For same example when we change the order of inputs it gives the following output:


Enter Name, RollNo, Marks, Grade
Maxon
Exception in thread "main" java.util.InputMismatchException
	at java.base/java.util.Scanner.throwFor(Scanner.java:939)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at javaFiles/FileHandling.ScannerExample.main(ScannerExample.java:14)


Here we get an InputMismatchException.