aspose file tools*
The moose likes Beginning Java and the fly likes Java noob -- sentinel-controlled while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java noob -- sentinel-controlled while loop" Watch "Java noob -- sentinel-controlled while loop" New topic
Author

Java noob -- sentinel-controlled while loop

Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
So I have seen this type of program on these boards a couple of times, but none with this specific issue. I also read that you all don't prefer going back into old threads, that it's a better idea to start a new thread with my own issues, so here it is.

I'm attempting to modify a program which will take user input for name, hourly pay, and weekly hours and compute the weekly pay. There also needs to be a "stop" sentinel incorporated into the program as well as verifying that the weekly hours and pay are not negatives.

I have come to the conclusion that a while loop will suffice on looping back to the employee name each time the program completes, but I am having issues. (I have not yet begun ensuring that the other variables are positive.)



The issue I am having is that when the program computes the total and goes back to loop to ask for the next employee's name, it prints but doesn't pause for user input but rather goes and outputs ""Please enter the hourly pay rate of the employee: " " and waits. I just cannot seem to figure out why.

Thanks in advance for any help, I really do appreciate the time.

Justin
Tarique Islam
Greenhorn

Joined: Oct 14, 2009
Posts: 5
you will need to break the loop if user input is "stop".
do something like this:



hope it helps
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Tarique Islam wrote:you will need to break the loop if user input is "stop". . .
That is incorrect. Apart from the fact that many of us (myself included) regard break as bad programming, it is completely unnecessary. You will never reach the break statement.

Justin Bodin: welcome to JavaRanch Please don't write things like n00b. Since you are new, I shall edit the thread title for you.

You are doing some things right and going about some things the wrong way. Unlike Robin Lane, you appear to believe what I tell you ( ), but if you are not sure about a loop, don't try to run a fully-functional loop. Try a loop like thisThat is what you posted with one amendment and lots of deletions. When you run that, you can see how it works and how the loop stops. The way to create a program is to start small. Then you can add bits until it works. Do it one tiny piece at a time. Add two lines, compile and execute. Then you can see how it develops and find errors one at a time rather than 69 at a time.

Why are you using float arithmetic? Double arithmetic is bad enough for imprecision and giving incorrect answers where precision is required; float is worse.
When you start getting no name printed out and InputMismatchExceptions, have a look at this post
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
And note you can use the %n tag after printf so you don't need a separate println statement.
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
Hey Campbell, thank you for the tips I really appreciate it.

But in the code you posted, at line 21 shouldn't there be an

otherwise, when the hourly wage, and hours worked are input, the loop will again SKIP the user's input and move on to asking for the weekly wage again?

That was one of the main problems with my code that I originally posted, If would have put that after looping back and asking for the next employee's name, the code would have at least been functional.

But yes, thank you for the tips. My instructor also agreed letting me know that using breaks are bad programming practice.

Oh, and I was unsure whether to use double or float so I read a bit about it. I some how understood that float executed more quickly, and was more suited for numbers with a small amount of decimal places (ie: 5.50) so I went with that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You're welcome

No, you don't need to add a call to myScanner. It is all in there. An awkward bit of syntax, which you would never work out for yourself if nobody showed you it. Inside the () after while, you have a call to input.nextLine(). There is no need for a second newLine call, which would simply discard the name entered first. Remember the loop has to go back to the () after while every run, and there is a call in those () so it will always be executed. Try it, in the form I posted, and you can see whether it works.

This trick will let you see about the break: Add the if () . . . break block again, but make it a proper block with {}. Just before the break; command, insert a line like this: System.out.println("Stop entered: goodbye.");. Then you can see whether you ever reach that block at all. Well done your teacher saying "break" is poor practice.

You are right that float arithmetic is about 4 times as fast as double arithmetic. Try it. Put in calls to the nanoTime method. Look for its full details in the System class.You can see it takes maybe 1μs to do nothing and longer if you do something real. You will also notice the times vary for no apparent reason. Get a stopwatch and see how long it takes from printing out "Enter name, or stop to finish" to entering "Campbell Ritchie".
The real problem with primitive fractional arithmetic is precision. Some books do tell you to use it, but it is not suitable for anything requiring precision, eg money. For money you should use integer arithmetic, or BigDecimal, but that is probably beyond the requirements of a beginner's exercise.
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
I did actually try it before I posted that, and it skips right over the user input.
It does make sense that the while loop should read the call to Scanner each time...I'm obviously doing something incorrectly.



Also, wouldn't you receive an input match exception pretty commonly if you use integer arithmetic for money?

Thanks again, I'm more grateful than I can express.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
If you use integer arithmetic for money, you would do your calculations in cents/pence, not $/£.
You are not missing the input at all. What you are doing is entering 12345↩ and the problem is exactly the same as I described here. Be sure to read that post. After entering the numbers, followed by "enter" you will need a nextLine() call.
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
Ah yes!



Thanks for your help again. And I did read that most of that thread, I just missed that post apparently.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Justin Bodin wrote:Ah yes!
. . .
Thanks for your help again. And I did read that most of that thread, I just missed that post apparently.
You're welcome

The point about newLine wasn't in the first thread you read, so you didn't miss it at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java noob -- sentinel-controlled while loop