aspose file tools*
The moose likes Beginning Java and the fly likes Frustating problem with Scanner nextLine() and next() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Frustating problem with Scanner nextLine() and next()" Watch "Frustating problem with Scanner nextLine() and next()" New topic
Author

Frustating problem with Scanner nextLine() and next()

Jan Engstrom
Greenhorn

Joined: Mar 29, 2013
Posts: 5
Hello fellows,

I tried for many hours solving this little problem. I coded a little example to show the problem.
Suppose i want to get userinput - name, lastname and birthday. If the user just presses enter the method should run again.
i think readLine() ignores the \n after the enter and the little bugger is left for next input, and the controlflow is not what i wan't...




This is my output from a run. I typed firstname and then just enter as lastname.
Birthday then appears twice.




If nextLine is changed to next then there is another problem.
Suppose i input Carl Gustav as firstname, it then jumps over lastname because of the space. (lastname becomes Gustav)



I am really stuck on this problem.

Thanks

Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
Am having a little headache trying to understand what you mean...do you want to continually ask for input as long as the entry is empty?
Jan Engstrom
Greenhorn

Joined: Mar 29, 2013
Posts: 5
Alan Anderson wrote:Am having a little headache trying to understand what you mean...do you want to continually ask for input as long as the entry is empty?


Yes if empty just run the method again.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1014
    
    5

Your problem here is that once you have called the method getInput() from within it, the flow continues where it left off. Also, you check if input is empty and if it is, you print it to console which seems a strange thing to do.
Jan Engstrom
Greenhorn

Joined: Mar 29, 2013
Posts: 5
James Boswell wrote:Your problem here is that once you have called the method getInput() from within it, the flow continues where it left off. Also, you check if input is empty and if it is, you print it to console which seems a strange thing to do.


Thanks for the reply. The reason it is printed to the console is only when i tried to solve the problem. Some sort of logging i guess.
I am also aware of the unchecked parse to Integer and that the yearOfBirth doesn't have a check for isEmpty.

The questions remains for me: how to get input from the console; allow space in a name or lastname (Firstname:Carl Gustav Lastname:Jung)
If empty run the method again. I also tried three smaller methods just to reenter the part that was empty. Same problem... For me it´s strange that this simple thing is hard to do.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38729
    
  23
There is a problem calling nextLine after anything other than nextLine, which you can read about here. That is particularly a problem if you think nextLine reads the next line, because it doesn’t.

I think you are going to have lots of repeated code if you program like that. I suggest you search my posts for “utility class” and you will find lots of utility classes, probably none of them complete. Put them together to create a utility class called KeyboardInput or similar with methods like nextIntFromKeyboard, nextLineFromKeyboardAfterSomethingElse, nextLineFromKeyboardOnItsOwn, etc. You can overload those methods to print different sorts of messages in case of incorrect inputs, and you can ensure incorrect inputs never make their way back to the calling class by using methods like hasNextInt. That might appear time‑consuming, but it will give you a resource you can use many times again.

And welcome to the Ranch
Jan Engstrom
Greenhorn

Joined: Mar 29, 2013
Posts: 5
Campbell Ritchie wrote:There is a problem calling nextLine after anything other than nextLine, which you can read about here. That is particularly a problem if you think nextLine reads the next line, because it doesn’t.

I think you are going to have lots of repeated code if you program like that. I suggest you search my posts for “utility class” and you will find lots of utility classes, probably none of them complete. Put them together to create a utility class called KeyboardInput or similar with methods like nextIntFromKeyboard, nextLineFromKeyboardAfterSomethingElse, nextLineFromKeyboardOnItsOwn, etc. You can overload those methods to print different sorts of messages in case of incorrect inputs, and you can ensure incorrect inputs never make their way back to the calling class by using methods like hasNextInt. That might appear time‑consuming, but it will give you a resource you can use many times again.

And welcome to the Ranch


Thanks CR,

So nextLine is not usable in this case. If there is more than one input the 'newline' is going to mess up the other read.
I was thinking maybe a solution is to write some sort of class: read the input in a loop/array letter by letter (bytes?) and then discard (but consume) the newline symbol.
Nothing in Javas methods that reads lines without leaving anything in the buffer? like nextLineAll()


Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
try using a do-while loop
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7771
    
  21

Jan Engstrom wrote:So nextLine is not usable in this case...

No, I don't think that's what he's saying. Personally, I always use nextLine(), because I just find it easiest.

It also guarantees a simple API:
  • Each piece of input is terminated by the user pressing the Enter key.
  • What they entered is validated based on what was asked for - either with something like regexes, or by using a Scanner on the resulting String.

  • But it's purely my personal preference - there are probably many ways of doing it - and I have to admit to not being a huge fan of Scanner to begin with.

    I also completely agree with Campbell that it's worth writing a utility class for this stuff.

    Winston

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38729
        
      23
    Winston Gutkowski wrote: . . . No, I don't think that's what he's saying. . . .
    Quite right. I am happy about using nextLine() (assuming you use Scanner at all), but, as that old post shows, you have to be very circumspect about nextLine() after a different Scanner method call.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Frustating problem with Scanner nextLine() and next()