Scanner s = new Scanner(System.in)
int x = S.nextInt();
double y = S.nextDouble();
S.nextLine(); //Why
String z = S.nextLine();
Why do I need to type S.nextLine()
after reading int & double before I can read a String ?
Scanner s = new Scanner(System.in)
int x = S.nextInt();
double y = S.nextDouble();
S.nextLine(); //Why
String z = S.nextLine();
Why do I need to type S.nextLine()
after reading int & double before I can read a String ?
If you have the following information
Line 1: 1.0 2.0 4.0(you are currently here after reading the double 4.0) \n
Line 2: (you are here once you eat that '\n' character using readLine())
Once you store the last digit: 4, you are still on that same line (line 1). You need to eat up that new line character. s.nextLine()
will read until the end of the line and return an empty result. Once that is done the scanner is positioned at the beginning of Line 2.