• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

while - Not accepting string input in the second round

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, ranchers!

I started Java recently - from scratch - no programming/ engineering experience (used to be German teacher), and need a lot of help.
I am doing problems from Deitel some things are confusing. I could work around, like omit some code - but it is not a solution, right?
Here is what I can't resolve: in while cycle, in the second round, it skips employee id and goes straight to the hourly rate. I can take the id thing away and the problem would be technically solved according to what is required by the author, but. The question remains.
I have a sneaking suspicion, it is about String and maybe cleaning buffer or something - but not sure. Can you help me?
Thank you in advance for your time



 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The culprit is in the "nextLine". Change it to "next" and will fix the problem.

Why is that? If you look at the JavaDoc for Scanner class, the "next" returns the next token, similar to the nextDouble, nextInt. The nextLine returns the line that was skipped and advanced the line.
 
Lena Sergeenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, K. ! It worked!
 
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is more explanation here. That will explain why you only have the problem second time round.

Please tell us what your book says about Scanner#nextLine.

And welcome again
 
Lena Sergeenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for welcoming me - it's nice to feel welcome - not every forum can boast hospitality like this!
And I've got pleeeenty of questions - you'll see me a lot :-)
As for the book covering Scanner nextLine - it should and it does for sure but not in the chapter I am in. They meant some other solution - without ID and string thing - I obviously bumped into this problem by not following conditions of the problem - story of my life, oh well
"Good judgment comes from bad experience ... And most of that comes from bad judgment." Zen teaching point :-)
 
Campbell Ritchie
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The more recent Deitel book which I have (Java for Progrmmers) says on page 61 that it uses the nextLine method to read a line and on page 62 the nextLine call read a line. Not actually wrong but it doesn't say what nextLine actually does: read from the current position to the end of the current line. The link I gave you yesterday shows an example where you get an empty String with nextLine.
 
Lena Sergeenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Java How to program, 10th edition.
You're right, in Ch 3 on page 75 it mentions method next and compares to nextLine - including the fact that method next does not return white space character, which is discarded as opposed to nextLine method, which discards newline and NOT white space character. To discover this subtle difference, you need to know what to look for, otherwise it is easy to leave it unnoticed - which I did. Thank you for this discussion - very helpful!!!
 
Campbell Ritchie
Marshal
Posts: 76450
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome I have yet to see a book which tells you how to deal with nextLine properly. Better solution: look in this post for a link. See if you can find my mistake before reading the whole of the thread.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic