[java] “Why this blank Scanner sh*t after each time of taking int or double as input?”

Well, by using java.util.Scanner we can take input from the user, it can be String, it can be integer, it can be double, even it can be a line!

to use java util.Scanner, we first import java.util.* package, then we declare an object of Scanner class:

Scanner sc = new Scanner (System.in);

Since System.in is an input stream, above declared Scanner will takes all necessary inputs from its input streams and System.in let the user give input streams through the system (maybe keyboard).

Various types of inputs can be taken as i have mentioned, can be taken via various next methods:

For example:

next() method is used to take input a word, it starts taking input from the streams and does not not stop until it spots character  a ‘ ‘ (space)!

Example: if the given input of the user is: “yo man”
then next() method will return “yo “, cause, after yo it spots a space ” ” and it refrains from taking any other inputs but in input stream still it has “man”  left next to “yo “, so if we again let next() method get called, then as input stream already has streams, so that next() won’t ask the user to give another input but it would take from the next input stream and so it would return “man” !

nextLine() takes input from the user until it reach an Enter. We have learned that <Enter> is nothing but a spacial character.

Example: users given input is: “yo man” and then press Enter
“How are you” and then press Enter
then nextLine() method will only return “yo man”, cause, after “yo man” it spotted an <Enter> and it refrains from taking any other inputs but in its input stream “How are you <Enter>” is still left!

nextInt() method takes integer inputs from the use and suppose now we call nextInt() method then it will start taking inputs from input stream which was left  before (i mean “How are you <Enter>”) and it would fail to get an Integer so
java.util.InputMismatchException will get thrown.

Now we will see a practical problem that we face due to this sort of thing:

import java.util.*;
public class Student{
Scanner sc=new Scanner(System.in);
System.out.print("Serial no: ");
int Serial =sc.nextInt(); //1st input from the user
System.out.print("Name: ");
String Name = sc.nextLine(); //won't ask the user to give an input

Why? Why? Why?
At the time when user gives an Integer when “Serial no: ” is asked, the user presses an integer number and then <Enter>.
nextInt() method returns that number and int Serial variable stores that intiger with no difficulty but in input stream <Enter> remains so at the time when nextLine() is being called (at the time when user is asked to give the “Name: ” ) it takes that <Enter> as a input from the input stream so user does not get any chance to enter the name.

Giving a blank sc.nextLine() after each sc.nextInt() can be a solution for this problem as String can hold anything.

System.out.print("Serial no: ");
int Serial =sc.nextInt(); /*user inputs a number and then presses <Enter> and int Serial stores only the number not that extra <Enter>*/
sc.nextLine(); /* it takes extra <Enter> */
System.out.print("Name: ");
String Name = sc.nextLine();

I think this blog post could be dedicated to a Friend with “Whys”, Fahim Bakhtiar, unlike any element of my so called GoodStudent class 😛 who has an unlimited amount of Wh-Questions to ask in his mind. And most probably I also fear what he would ask again regarding this issue where i will get stumbled miserably 😛 but he is always welcome as always! All the best to him!

Disclaimer: This is what i have assumed about Scanner class. Correct me if i am wrong! Suggest me good articles about this Scanner as i have got nothing.