Scanner in java:

Java User Input:

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

Why we use Scanner:

Scanner is a class in java. util package used for obtaining the input of the primitive types like int, double, etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.

What is (System.in)Scanner class:

Scanner class allows user to take input from console. System.in is passed as a parameter in Scanner class. It tells the java compiler that system input will be provided through console(keyboard).

What is the benefits of scanner class:

The advantages of Scanner Class are: The end of data element can be determined through a special token. It not only reads data but also parses it into specific types like short, int, float, boolean, etc. It can read String as well as primitive data types.

Example program:

package scanner;


import java.util.Scanner;

public class Smart
{

public static void main(String[] args)
{
	Scanner sc = new Scanner(System.in);
	System.out.println("enter number");
	int no = sc.nextInt();
	System.out.println(no+10);
}
}

output:

Leave a Comment