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

Thread: scanner.nextLine()

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post scanner.nextLine()

    Hey, plz can u explain what scanner.nextLine() means...

    Thanks

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

    Default Re: scanner.nextLine()

    That statement shows a object reference: scanner being used to call a method: nextLine().
    To see what that method does, read the API doc for the scanner class.
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scanner.nextLine()

    can u give a real life example of the scanner.nextLine() method please?

    it would help me understand it :p

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

    Default Re: scanner.nextLine()

    Do a search here on the forum. There are lots of programs that use that method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scanner.nextLine()

    but can you give me a real life example?

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

    Default Re: scanner.nextLine()

    The code posted here on the forum are real life examples of the usage of that method.

    If you have any questions about the code you have found and can not get it to compile and execute for testing, copy the copy here and ask any questions you have about it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scanner.nextLine()

    does scanner mean that you can type stuff?





    Scanner scanner1 = new Scanner (System.in);

    System.out.println("Can you type?");
    scanner1.nextLine() //with this i can type and characters will appear - without this line no characters i type will appear.

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

    Default Re: scanner.nextLine()

    Did you read the API doc for the nextLine() method to see what it does? It is a method that returns a String.

    type and characters will appear
    Are you talking about what appears on the console window when you type something in?

    does scanner mean that you can type stuff?
    The use of the System.in arg in the constructor designates where the Scanner class will get its input from: the keyboard.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scanner.nextLine()

    Ok, yes i typed in the console window

    and is that a yes for does scanner mean that you can type stuff?

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

    Default Re: scanner.nextLine()

    I'm not sure what you mean by "type stuff". When I open a command prompt window, I am able to type stuff into the window. It has nothing to do with java or using the Scanner class.

    Using the Scanner class with System.in as its arg means that what the user types into the command prompt window will be read by the Scanner class's methods.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scanner.nextLine()

    Ok, basically when i take away the "scanner1.nextLine()"
    characters dont appear in the console when i type them,
    If scanner1.nextLine() is added, i can type characters into the console and they will show up.

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

    Default Re: scanner.nextLine()

    Interesting. I don't know what use typing characters on the console is.
    The useful part is that what is typed on the console can be read into the program and used by the program.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scanner.nextLine()

    I use eclipse to type in the console.

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

    Default Re: scanner.nextLine()

    I use the OS's command prompt window.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Dont quite understand how Scanner.nextLine() works
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: September 21st, 2011, 05:27 PM
  2. name=scan.nextLine();
    By Amro in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 12th, 2010, 07:01 AM
  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