Rommel Rico: Random ramblings

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!

  • 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.

  • Glad I was able to help, aga. :)

  • Thanks, finally got this working. Teacher says: ‘Think logically’. This makes no sense! Why isn’t it working! No logic is present.

  •   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.

  • Thank you for your extremely great info and feedback from you. san jose used car 915426

  • I

  • Outstanding post, I believe weblog owners need to larn a whole lot from this site its rattling user friendly . 19673

  • dog grooming will be the specialty of my sister, she truly loves grooming every dog in our house 703410

  • Very educating story, saved your site for hopes to read more! 323330

  • It

  • I just couldn

  • Great info many thanks sharing and reaching us your subscriber list. 887157

  • 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

  • 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?

  • 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”

You can follow any responses to this entry through the RSS 2.0 feed.