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 15 of 15

Thread: Dont quite understand how Scanner.nextLine() works

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dont quite understand how Scanner.nextLine() works

    Hi,

    see here:

    firstChoice = new Scanner(System.in);
     
    		System.out.println("You chose " + firstChoice.nextLine());

    I know what happens, I'm giving my Scanner object the parameter of the keyboard input and then I am outputting "You chose" plus what the user has typed in. My question is what exactly does the nextLine method do to print out my Scanner objects parameter?

    I have read it "advances this scanner past the current line and returns the input that was skipped.", as I'm a beginner could you explain in simply terms what this method does to display the objects parameter.

    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    what exactly does the nextLine method do to print out my Scanner objects parameter?
    The nextLine method returns a String value. It does not print out anything. Where it gets that String is determined by how the Scanner class is created. It can read from the console, from a String or from a disk file.
    The System.out.println method does the printing.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    Thanks for the reply.

    I understand that system.out.print is doing the actually printing.

    Im still confused, answer me this, when I put...

    firstChoice = new Scanner(System.in);
     
    		System.out.println(firstChoice.nextLine());

    ...it prints out what i type in, but when i put...

    firstChoice = new Scanner(System.in);
     
    		System.out.println(firstChoice);

    ...it prints "java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]".

    So by using the nextLine() method, it changes the output from that to what I actually input.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    firstChoice is an instance of the Scanner class. When you print it, the instance's toString method returns information about how the Scanner class instance has been initialized. It has nothing to do with the data that the Scanner object is reading.

    by using the nextLine() method, it changes the output
    Read the Scanner class's API doc for the nextLine method to see what it returns.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    Its doc say:

    "Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
    Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.


    Returns:
    the line that was skipped"

    How does nextLine() method change the output?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    How does nextLine() method change the output?
    Where is the "output" you are asking about?
    The nextLine method returns a String.
    Inside of the Scanner class instance there are pointers and buffers that handle what the user has typed in at the console. Those will be changed when nextLine is called.

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    Just checking, when we print a method by putting "System.out.print(exampleMethod);" are we printing the methods return value?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    when we print a method by putting "System.out.print(exampleMethod);
    That code is NOT a method call. To call a method you put () after it:
    System.out.print(exampleMethod());
    are we printing the method's returned value?
    Yes.

  9. #9
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    Oh yeah, I forgot to add the brackets then, my bad

    Thanks, its clearer now.

    What exactly is returned from nextLine tho, is it the object parameter, which was the keyboard input we gave it earlier?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    What exactly is returned from nextLine
    Two places to see:
    1) read the API doc
    2) execute it, save it what it returns in a variable and print out the variable or examine the variables contents with some methods.

  11. #11
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    Am I right in saying basically this nextLine() method is made to pause and wait until it gets input from the scanner objects parameter. If the objects parameter already has input such as ("hi") then it will output that, however if the objects parameter is (System.in) then it will wait until the user enters text.

    Right?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    Try writing a small test program and try all the combinations that you can think of to see how it works.

  13. #13
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    I have played with it, and thats what ive come with, was i wrong then? :S

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    What you said sounds about right.
    I can't argue with a program's results. If you have a program that you don't understand what it is doing, post the program and the console where you executed it and your questions.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  15. #15
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dont quite understand how Scanner.nextLine() works

    Thanks

    I know how to use it but i was just trying to find out its inner workings

Similar Threads

  1. I dont understand why this happens....
    By ashenwolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 09:31 PM
  2. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 09:31 PM
  3. Question concerning nextLine() method in Scanner Class
    By r_sardinas in forum Java SE APIs
    Replies: 2
    Last Post: October 20th, 2010, 08:35 PM
  4. I need help! Re: strings, nextLine, while and more..
    By rockerade in forum What's Wrong With My Code?
    Replies: 19
    Last Post: October 5th, 2010, 03:51 PM
  5. Replies: 1
    Last Post: August 13th, 2010, 06:58 AM