Question concerning nextLine() method in Scanner Class
Here's my dilemna.
I'm a teacher's assistant for one of our beginner classes (CPSC 1301). I've done this for several semesters now. When students are learning the Scanner class, there are often a few of them that will try to use the nextLine() method in their code, then, while still using the same scanner, try to use another method (perhaps scan.next()), and the result is that the next method does not happen, but gets skipped. This doesnt happen with the other Scanner methods, and i was wondering if someone could explain the technical details of what the nextLine() method actually did, so i could give the students some reason why it doesnt work, instead of just saying, " just don't use that here".
The teacher can't explain it either.
So:
1) Make a scanner object
2) attempt to use it several times
3) use nextLine() method
4) next Scanner method attempted gets skipped
Example Code
Code java:
import java.util.Scanner;
public class Help {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Using next");
scan.next();
System.out.println("Using nextLine");
scan.nextLine();
System.out.println("Using nextAnything");
scan.nextInt();
System.out.println("Using nextAnything here");
scan.next();
}
}
Result:
================================================
Using next
something
Using nextLine
Using nextAnything
anything
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Help.main(Help.java:20)
==============================================
Thanks in advance for any help,
Rodrigo
Re: Question concerning nextLine() method in Scanner Class
This is based upon the fundamental way input is read from the command line, and how the input is parsed. System.in blocks until input is received...which is a new line. Based upon your example, the "Using next" call blocks until input is received (the user hits return). The next() will read in characters based upon its delimiter (which is by default whitespace) UP TO the delimiter (which in turn leaves that delimiter for future calls). So in this case a new line is left for the call to nextLine() to parse (which is by then an empty string). Then the following call will block until input is received again.
Re: Question concerning nextLine() method in Scanner Class
Ah, thanks so much. I changed what i typed in this time and printed out what the results were and they demonstrate exactly what you said.
New Code:
Code java:
import java.util.Scanner;
public class Help {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Using next");
System.out.println("next info: " + scan.next());
System.out.println("Using nextLine");
System.out.println("nextLine info: " + scan.nextLine());
System.out.println("Using nextAnything");
System.out.println("nextInt info: " + scan.nextInt());
System.out.println("Using nextAnything here");
System.out.println("next info: " + scan.next());
}
}
Result:
Using next
Something goes here <-- I typed this
next info: Something
Using nextLine
nextLine info: goes here
Using nextAnything
12 <-- I typed this
nextInt info: 12
Using nextAnything here
Something goes here <-- i typed this
next info: Something
Thanks very much for the quick reply also.
Rodrigo