Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with looping... Arg

 
Debra Simeroth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem that seems all too familiar, I have gotten quite a bit of help for this in the forums already. But try as I might, I cannot declare the variable employeeName at the beginning of the file, which I suspect is part of my problem. When I try to declare the string as private, I am given a "illegal start of expression" error message. I cannot figure out what my problem is here, and am not able to get the employeeName entered at the end of the loop, or to have it be read properly. What I have this far compiles, but I am not able to re-enter the employee name again to re-evaluate the loops condition.



Any help would be extremely appreciated. This is still last weeks homework and I am stuck!

Thanks!
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot figure out what my problem is here, and am not able to get the employeeName entered at the end of the loop, or to have it be read properly. What I have this far compiles, but I am not able to re-enter the employee name again to re-evaluate the loops condition.


It is a good idea -- for debugging purposes -- to print out what you actually got from the scanner, so you have an idea of what is going on. Can I assume that you tried that already?

If I had to guess at the problem though, I am guessing that you are getting a blank employee (after the first one) -- probably caused by the fact that you are reading the CR/LF at the end of the last double that was read.

Henry
 
Debra Simeroth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
I cannot figure out what my problem is here, and am not able to get the employeeName entered at the end of the loop, or to have it be read properly. What I have this far compiles, but I am not able to re-enter the employee name again to re-evaluate the loops condition.


It is a good idea -- for debugging purposes -- to print out what you actually got from the scanner, so you have an idea of what is going on. Can I assume that you tried that already?

If I had to guess at the problem though, I am guessing that you are getting a blank employee (after the first one) -- probably caused by the fact that you are reading the CR/LF at the end of the last double that was read.

Henry


Hi Henry!

I am very new at this... this is an assignment from the 3rd week of class. When you refer to the scanner, are you referring to what is output when the program is run, or the error messages? Also, what is the CR/LF? I am sorry for being so unknowledgeable. Thanks for the help.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you refer to the scanner, are you referring to what is output when the program is run, or the error messages?

No, he's referring to the Scanner object in your code, called "input". Have frequent System.out.println(...) statements sprinkled through your code that show the values of the Strings that the Scanner object has read in (or any other pertinent variables in your code).

Also, what is the CR/LF?

Carriage-return / Line-feed: these are the tokens present at the end of lines (in some operating systems).
 
Kumar Kamlesh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just add

input = new Scanner( System.in ); //at line 58

before you are doing

employeeName = input.nextLine(); // read a line of text

in the loop.

Hope its not too late!

- Kamlesh
A Student of Java
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar Kamlesh wrote:Just add

input = new Scanner( System.in ); //at line 58

before you are doing

employeeName = input.nextLine(); // read a line of text

in the loop.


Isn't this like "throwing the baby out with the bath water" ? Instead of throwing away the scanner (on every iteration), would it not be better to get rid of the extra characters that the scanner is still holding -- before prompting for the next input?

Henry
 
Kumar Kamlesh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right. Actually I have not got a chance to use Scanner class. It is just what I tried in order to identify and solve the problem. Thanks for pointing it out.

 
Varun K. Gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use input.next() instead of input.nextLine(), problem will get solved
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic