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

Thread: What is the difference between next() and nextLine()?

  1. #1
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is the difference between next() and nextLine()?

    Can you tell me the difference between those two?
    I have noticed that when i use next(), for example:
    String entered_name=scan.next(); , I can enter only one word, this method doesn`t accept two words separated by space
    On the other hand when i use nextLine(), for example:
    String entered_name=scan.nextLine(); , I am not allowed to enter anything, the program just skips this step and entered_name remains empty.

    What I need is to be able to enter several words separated by space.
    What is the difference between those two and what is the solution to my problem?

    Thanks in advance!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is the difference between next() and nextLine()?

    Have you read the Scanner class's API doc? Do you have any questions about the descriptions there of those two methods?
    Please post your questions here.
    A token is a string of non blank characters surrounded by white spaces.
    The current line ends with a newline character.

    Can you post the program you are having trouble with?

    What I need is to be able to enter several words separated by space.
    Use the nextLine method to read all of what is on the current line. It is possible for the current line to only have a newline character which would return an empty string.

Similar Threads

  1. Need to know this difference
    By arvind in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2010, 06:24 AM
  2. Difference between Vector and Arraylist
    By Lil_Aziz1 in forum Collections and Generics
    Replies: 4
    Last Post: January 3rd, 2010, 11:03 AM
  3. Difference between Speech API and Sound API
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: October 22nd, 2009, 12:22 AM
  4. nextLine problems, any help appreciated
    By Schmitz14 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 5th, 2009, 01:23 AM
  5. 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