aspose file tools*
The moose likes Beginning Java and the fly likes Problem with looping... Arg Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with looping... Arg" Watch "Problem with looping... Arg" New topic
Author

Problem with looping... Arg

Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
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
Sheriff

Joined: Sep 28, 2004
Posts: 18846
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: May 10, 2009
Posts: 15
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
Sheriff

Joined: Sep 28, 2004
Posts: 18846
    
  40

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

Joined: May 10, 2009
Posts: 15
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

Joined: Jun 14, 2009
Posts: 10
use input.next() instead of input.nextLine(), problem will get solved
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with looping... Arg