Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Question concerning nextLine() method in Scanner Class

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Location
    Columbus, Ga
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
     
     
    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


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,103
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

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

  3. The Following User Says Thank You to copeg For This Useful Post:

    r_sardinas (October 20th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Location
    Columbus, Ga
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up 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:

     
    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

Similar Threads

  1. Alternative to the scanner method
    By Harrisk9 in forum Java SE APIs
    Replies: 2
    Last Post: November 28th, 2009, 09:30 PM
  2. Scanner class error "java.lang.Error"
    By Lheviathan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2009, 02:23 AM
  3. [SOLVED] Problem in Coin-counter with scanner class
    By coccoster in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 25th, 2009, 08:46 AM
  4. problem with Scanner nextLine() method
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 10th, 2009, 04:14 AM
  5. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM

Tags for this Thread