Scanner class in Java

Lesson Progress
0% Complete

The Scanner class is used to get user input, and it is found in the java.util package.

To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation.

In our example, we will use the nextLine() method, which is used to read Strings:

Example Code for Java Input

import java.util.Scanner;

public class InputValue {

	public static void main(String[] args) {
		Scanner sc = new Scanner(;
		System.out.println("enter ur roll no");
		int rollno = sc.nextInt();
		System.out.println("ur roll no is" +rollno);