1import java.util.Scanner;
2
3// import scanner
4
5Scanner myScanner = new Scanner(System.in); // Make scanner obj
6
7String inputString = myScanner.nextLine(); // Take whole line
8
9boolean inputBoolean = myScanner.nextBoolean(); //Boolean input
10
11long inputLong = myScanner.nextLong(); //Interger,long ... input
1import java.util.Scanner; //Import Scanner in java
2
3class classname{
4 public void methodname(){
5 Scanner s_name = new Scanner(System.in); //Scanner declaration
6 //Use Scanner object to take input
7 int val1 = s_name.nextInt(); //int
8 float val2 = s_name.nextFloat(); //float
9 double val3 = s_name.nextDouble(); //double
10 string name = s_name.nextLine(); //string
11 char ch = s_name.nextLine().charAt(0); //character
12 }}
1The Java Scanner class is widely used to parse text for strings
2and primitive types using a regular expression.
3It is the simplest way to get input in Java.
4By the help of Scanner in Java, we can get input from the user
5in primitive types such as int, long, double, byte, float, short, etc.
6
7
8Scanner in = new Scanner(System.in);
9