Results 1 to 4 of 4
  1. #1
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Scaner NextLine()

    Hey could someone explain to me why Info will print/ scan a blank space for Info? I've looked at the scanner class, but still cant seem to get my head around it.(Each token is on its own line)


    Java Code:
     
          Width = scan.nextInt();
          System.out.println(Width);
          
           
          Height = scan.nextInt();
          System.out.println(Height);
          
           
          Info = scan.nextLine();
          System.out.println(Info);
    Thanks.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Scaner NextLine()

    I really must store this reply somewhere...

    The nextInt() method (and the related next<primitive> ones) will clear the buffer before reading in the int from the console.
    In reading in the int they leave behind the new line character(s).
    So:
    nextInt(); // Read in an int
    nextInt(); // clears the new line left behind by the above and then reads in the int

    Now, nextLine() is different. It does not clear the buffer.
    So:
    nextInt(); // read in int, but remember there's still a new line in the buffer.
    nextLine(); // Reads in that new line...and nothing else.

    So, the simple solution is to call nextLine() after the last nextInt() in order to clear the left over new line in the buffer.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: Scaner NextLine()

    Quote Originally Posted by Tolls View Post
    I really must store this reply somewhere...

    The nextInt() method (and the related next<primitive> ones) will clear the buffer before reading in the int from the console.
    In reading in the int they leave behind the new line character(s).
    So:
    nextInt(); // Read in an int
    nextInt(); // clears the new line left behind by the above and then reads in the int

    Now, nextLine() is different. It does not clear the buffer.
    So:
    nextInt(); // read in int, but remember there's still a new line in the buffer.
    nextLine(); // Reads in that new line...and nothing else.

    So, the simple solution is to call nextLine() after the last nextInt() in order to clear the left over new line in the buffer.
    Thanks Tolls, that does make sense now!
    One question though If I test something for a *edit* string such as

    Java Code:
     
    if(scan.nextLine().equals("null")){
       }
    will my scanner move down a line?
    Last edited by Dex; 08-14-2012 at 11:49 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Scaner NextLine()

    Yes.
    It has to consume up to the next new line.
    That's what nextLine() does.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. problems with .next() .nextLine(); Loop
    By zniightmare in forum New To Java
    Replies: 16
    Last Post: 02-12-2012, 03:32 AM
  2. nextLine issue
    By paul1024 in forum New To Java
    Replies: 3
    Last Post: 03-25-2011, 12:48 AM
  3. input nextLine error
    By scoobyrox in forum New To Java
    Replies: 6
    Last Post: 02-25-2011, 02:08 AM
  4. input.nextLine();
    By Cass29 in forum New To Java
    Replies: 3
    Last Post: 12-21-2009, 08:43 PM
  5. .nextLine(); only picks up first word
    By ethanemc505 in forum New To Java
    Replies: 1
    Last Post: 10-08-2009, 08:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •