1.

Solve : Java program not looping as intended.?

Answer»

Note:

I want a nice hint. Please DO NOT just tell me what to put. Yes, this is for a class.

My teachers (intelligible) advice was "There is garbage in the buffer. Read another line before reading the employee name." Which I can make no sense from.

I have a java program I am trying to repeat while a CERTAIN condition is true.

There are three sections.

First, it prompts for a name. If the name is "Stop" the program quits. If not, it continues.

Next, it prompts for pay rate and hours worked. If number is negative, it prompts for positive number.


It is supposed to repeat all three sections until the Name is "Stop". HOWEVER, it skips the name entry section when repeating.

Can someone give me a hint or advice as to where to look for the error? I am still trying to learn.

Thanks!

[recovering disk space, attachment deleted by admin]Writing out the flow of how this will all work as a flow chart before programming it will make it easier, you can then ADD comments within your program and note them on your flow chart on paper. You then program it up and when something goes wrong instead of looking at raw code, you will have a map of what should be happening with comments in the code to relate to each piece of this. This is extremely beneficial later on when working with much larger projects than this.

Having all inputs contained within a loop with a counter would be my advice. After all input has been entered. You test for stop. If stop is found it ends program. If stop is not entered and other information is input, then you increment the counter++ to break out of the input information while loop. My guess is that you will want NESTED loops which allow for you you pass the user in and out of loops for additional entries etc vs having to run the program again from scratch to be able to enter the next info. To get back into the nested while loop you can have an IF statement that changes the value of this counter back to say 0 in which 1 is the exit for the loop, forcing the value from 1 back to 0 will cause for you to be able to enter the next entry of info.

Tricky trying to explain this without just posting a code example that is more straight forward, but I think you will figure it out from what I am sharing for info.

As far as garbage in the buffer. This is corrected by reinitializing the variables back to null or some other value so that there is no possibility that information ( garbage ) can be inherrited into another portion of your program. As far as the employee name comment, I cant make sense of what the teacher was getting at, other than the employee name entry would be the perfect location to test the input string for "stop".Quote

My teachers (intelligible) advice was "There is garbage in the buffer. Read another line before reading the employee name." Which I can make no sense from.
Interestingly, their solution to read another line is correct, though they do poorly at explaining why. There isn't really "Garbage" in the buffer. What is happening is that nextFloat() and similar functions don't read passed the next carriage return, so they 'leave it' in the input buffer. So when you enter the number of hours worked that Carriage return is still there; nextLine() will have input to read right away.

You can either call nextLine() immediately after reading via nextFloat() each time to swallow the carriage return or use nextLine() to read in a string for the numeric inputs and use Float.parseFloat() to TRY to convert them. The latter will require exception handling so the former would probably be easier for this instance.


Thank you both very much.

I got the program to start repeating correctly thanks to you guys, and I understand much better now!

YAY!

Thanks again!


Discussion

No Comment Found