This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem during second loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem during second loop" Watch "Problem during second loop" New topic
Author

Problem during second loop

brian alverio
Greenhorn

Joined: Aug 21, 2012
Posts: 1
Im building a payroll program that uses a while loop to stop the program when the word stop is entered in - everything works fine until the second loop, on the second loop it ask for the employee name and payrate all on the second line, any help would be appreciated. Code is below:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Follow that loop very carefully with a pencil and a sheet of paper. Work out how often you are asking for the employee’s name per iteration.

And welcome (again).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Also beware of nextLine() which does something slightly different from what you think it does. But I won’t tell you about it. I shall refer you to an old thread where somebody else had problems with it. That might be the cause of the problem … if you had told us what the problem was
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Don’t use \n, least of all when using printf. Use %n. Use %n%n rather than the separate println call.
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

hi Brian,

The problem was in your code with input.nextline() . This works good.



Regards
Jatan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
jatan bhavsar wrote:. . . This works good. . . .
No, it doesn’t. It would not accept Campbell Ritchie as a name.

I shall let you work out what you have done wrong.
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

Hi ,

I got my Mistake it gives Exception when enter name with the space.



Above code will take the user inputs .

Sorry for the wrong Information..This is time i checked it properly.

Regards
Jatan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
If you are using a BufferedReader, youwill need a try-finally to ensure it is closed (or a try‑with‑resources). I meant that your code did not sort out the nextLine problem. You are also not stopping as required:
java Payroll
Enter Employee Name: Campbell
Enter the employee pay rate:12.34
Enter the hours worked this week: 42.5
Employee Name is Campbell
Weekly pay Amount is $524.45

Enter Employee Name: stop
Enter the employee pay rate:99
Enter the hours worked this week: 12
Employee Name is stop
Weekly pay Amount is $1188.00
Get a piece of papaer and a pencil and go through the loop and work out carefully where it starts and stops, so you can get it to stop when you enter stop.

Hint: there is an awkward syntax which allows you to enter a name and check its equality to stop, all inside the () after while. If you search long enough for payroll classes, you might find somebody else on the Ranch who used that syntax.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem during second loop
 
Similar Threads
Identifier expected
Problem with looping... Arg
condition to leave all your loops
Nested while loop inside do...while loop?
Code problems