File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Frustating problem with Scanner nextLine() and next()

 
Jan Engstrom
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using a do-while loop
 
Winston Gutkowski
Bartender
Pie
Posts: 9461
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47229
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic