Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

while - Not accepting string input in the second round  RSS feed

 
Lena Sergeenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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



 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks, K. ! It worked!
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • 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
  • 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: 55687
162
  • Mark post as helpful
  • send pies
  • 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
  • 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: 55687
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!