One of the most interesting topic of java is the scanner Class and it's use. While doing programming, the most important duty of a programmer is to take input of user. We can't use literals all the time hence we need a medium to take input from user and provide the required output.
Hence, scanner is very preferable to use for this purpose. Although buffer reader is also present but scanner is quite efficient, easy and effective to use.
For using it, there are given its syntax and as I have stated in one of my video that we need an import statements to apply these specific classes.
Syntax :- Scanner sc = new Scanner (System.in)
Import statement :- import java.util. *;
Important syntax to take input in various variables.
For taking input in integer we will firstly define a variable like int a or we can define it simultaneously while writing the input syntax, now the syntax will be written like this,
1. a= sc.nextInt ();
Similarly for others, it would be same.
1 . long a = sc.nextLong ();
2. short a = sc.nextShort ();
3. float a = sc.nextFloat ();
4. double a = sc.nextDouble ();
5. string a = sc.next (); (for taking only a word only).
6. string a = sc.nextLine (); (for taking a line or statement).
Note -> Char (character) is not allowed in scanner. For this, you can use buffer reader. We will write over this also.
For deep understanding you can see my video,
https://www.youtube.com/watch?v=J8FlockRNVM&app=desktop