Results 1 to 8 of 8
  1. #1
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    17
    Rep Power
    0

    Default .hasNext() or .hasNextLine()? Which to use?

    Im making a simple program, but I cannot tell which to use..

    Ive been using .hasNextLine() but .hasNext() also works and I would like to know the difference.

    I cannot seem to find the difference between the two, and I would like to know for future reference.


    Thanks.. Simple terms please :)

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

    Default Re: .hasNext() or .hasNextLine()? Which to use?

    ... on which class?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    17
    Rep Power
    0

    Default Re: .hasNext() or .hasNextLine()? Which to use?

    Quote Originally Posted by gimbal2 View Post
    ... on which class?
    Does it matter? I dont know how to explain that sorry.

    I am trying to use a scanner to indentify what the user put into the console.

    if(userInput.hasNextLine()){

    this.setName(userInput.nextLine());


    }

    But why use .hasNextLine and not .hasNext()?

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

    Default Re: .hasNext() or .hasNextLine()? Which to use?

    ... so its the Scanner class. Yes of course it matters. If I translate your question into this:

    "I press the red button, but it also has a blue button. Which one should I use?"

    Would you be able to answer it without knowing what the buttons are attached to first?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: .hasNext() or .hasNextLine()? Which to use?

    Quote Originally Posted by DayKema View Post
    Does it matter? I dont know how to explain that sorry.

    I am trying to use a scanner to indentify what the user put into the console.

    if(userInput.hasNextLine()){

    this.setName(userInput.nextLine());


    }

    But why use .hasNextLine and not .hasNext()?
    hasNext is written to work with the next() method.
    hasNextLine is written to work with the nextLine() method.

    The next() method works with whatever token is used as your separator. By default Scanner uses whitespace, so:
    This is several tokens
    would be 4 different calls to next(), one for each word.

    nextLine() reads a line of text, so would read the above example all in one go.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    17
    Rep Power
    0

    Default Re: .hasNext() or .hasNextLine()? Which to use?

    Quote Originally Posted by gimbal2 View Post
    ... so its the Scanner class. Yes of course it matters. If I translate your question into this:

    "I press the red button, but it also has a blue button. Which one should I use?"

    Would you be able to answer it without knowing what the buttons are attached to first?
    Obviously you never press the red button. Everyone knows that. So of course you would press blue..

  7. #7
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    17
    Rep Power
    0

    Default Re: .hasNext() or .hasNextLine()? Which to use?

    Quote Originally Posted by Tolls View Post
    hasNext is written to work with the next() method.
    hasNextLine is written to work with the nextLine() method.

    The next() method works with whatever token is used as your separator. By default Scanner uses whitespace, so:
    This is several tokens
    would be 4 different calls to next(), one for each word.

    nextLine() reads a line of text, so would read the above example all in one go.
    Ahh your genius. Thanks.

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: .hasNext() or .hasNextLine()? Which to use?

    Quote Originally Posted by DayKema View Post
    Obviously you never press the red button. Everyone knows that. So of course you would press blue..
    So you have no idea what a Class is and then you jokingly dismiss a perfectly good example of why you need to know what a Class is and which one you are using. You may have a tough time learning Java.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. what is this error on hasnext code in webgraph
    By javakar in forum Web Frameworks
    Replies: 2
    Last Post: 03-07-2014, 10:47 PM
  2. hasNext, Ctrl-Z not working
    By kahwawashay in forum New To Java
    Replies: 15
    Last Post: 05-12-2012, 09:16 AM
  3. Need help with Scanner/hasNext
    By Lutro in forum New To Java
    Replies: 1
    Last Post: 03-18-2012, 12:40 AM
  4. hasNext call
    By scotts in forum New To Java
    Replies: 4
    Last Post: 06-20-2010, 03:58 PM
  5. Using scanner.hasNext() but recognize return
    By ScKaSx in forum New To Java
    Replies: 5
    Last Post: 01-27-2009, 10:11 AM

Posting Permissions

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