• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Problem with looping... Arg

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23943
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23943
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use input.next() instead of input.nextLine(), problem will get solved
 
Space pants. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic