• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Frustating problem with Scanner nextLine() and next()

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try using a do-while loop
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
    reply
      Bookmark Topic Watch Topic
    • New Topic