Java Scanner not working, difference between next() and nextLine()
When I was a newbie programmer, I used to be frustrated a lot every time I used the Scanner class. At the time, it seemed like it behaved erratically and no matter how much debugging I did, it was always wrong.
The mistake I was making was that I failed to realize that whenever I used a .next call, like nextInt or nextDouble, the Scanner would stay on the same line. If then, I wanted to scan a new input that was on a different line, the Scanner will appear to skip it. What happens is that the Scanner is actually scanning the invisible newline character!
So, whenever you want to Scan numbers with a .next() call, make sure you add an empty .nextLine() at the end so that the Scanner moves to the correct position.
Ex:
import java.util.Scanner;class myClass { public static void main(String[] args) { Scanner scn = new Scanner(System.in); double grade=0; String name=null; System.out.print("Please enter the grade: "); grade = scn.nextDouble(); //Could also be scn.next(), scn.nextInt(), and so on //Add an empy .nextLine() to skip the invisible newline character scn.nextLine(); //Try commenting this line out with '//' to see what happens System.out.print("Please enter name: "); name = scn.nextLine(); System.out.println("Name: "+name+", grade: "+grade); } }
Sample run without the empty scn.nextLine:
Please enter the grade: 76
Please enter name: Name: , grade: 76.0
Sample run with the empty scn.nextLine:
Please enter the grade: 100
Please enter name: Rommel
Name: Rommel, grade: 100.0
Beautiful!
Hi, my name is Rommel Rico. I work as a Sr. Programmer Analyst at the University of San Diego. My passions include entrepreneurship, computers, languages, football (soccer), art, reddit, theater, science fiction, classical music, aviation, running, and video games. At the moment, I'm really into StarCraft 2.
aga
8 Nov, 2011
Thank you very much, man! I spend about one hour, trying to figure out what’s going on and why I’m getting continuous loop, while working with Scanner.next(). You saved me a lot of time, so thank you again.
Rommel
9 Nov, 2011
Glad I was able to help, aga.
Dominic
7 Dec, 2011
Thanks, finally got this working. Teacher says: ‘Think logically’. This makes no sense! Why isn’t it working! No logic is present.
Rosetta Stone Italian
12 Mar, 2012
This is a good,common sense article.Very helpful to one who is just finding the resouces about this part.It will certainly help educate me.
เกมส์ ป
13 Mar, 2012
Thank you for your extremely great info and feedback from you. san jose used car 915426
galaxy s3
14 Mar, 2012
I
Venapro
17 Mar, 2012
Outstanding post, I believe weblog owners need to larn a whole lot from this site its rattling user friendly . 19673
assurance auto
18 Mar, 2012
dog grooming will be the specialty of my sister, she truly loves grooming every dog in our house 703410
hardwoodflooringcost.org
19 Mar, 2012
Very educating story, saved your site for hopes to read more! 323330
Tarzan vibrator
20 Mar, 2012
It
hardwoodflooringcost.org
20 Mar, 2012
I just couldn
x ray technician salary
21 Mar, 2012
Great info many thanks sharing and reaching us your subscriber list. 887157
sizegenetics
25 Mar, 2012
I ought to admit that this really is 1 wonderful insight. It surely gives a company the opportunity to get in on the ground floor and genuinely take part in creating something particular and tailored to their needs. 738572
Ngan Marmolejo
30 Mar, 2012
Thank you, I’ve just been searching for information about this topic for a while and yours is the greatest I’ve discovered till now. But, what in regards to the conclusion? Are you sure concerning the supply?
Rafael
5 May, 2012
mmmm testing this, i found a error in your code that is “name = scn.nextLine();”….that line is why you get the error…it has to be only name = scn.next(); and you won’t need the scn.nextLine(); below the “scn.nextDouble”