Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem during second loop

 
brian alverio
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 297
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Brian,

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



Regards
Jatan
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 297
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic