• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to terminate this program on a while loop?  RSS feed

 
Gess Bennett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone! I am hoping to get a little help with a program that I am writing. I am almost finished, I just can't figure out how to use a while loop with a sentinel value, so that the user can terminate the program at anytime, and so that the program will not end until the user enters the value. I've thought long and hard and tried many different ways that just didn't work.

Here are the conditions:

-Your program will execute until all salesperson’s paychecks have been calculated
-use a sentinel loop to end the program

This is a really long code, but I added a lot of comments for navigation. I think only the top matters in this case anyway. Thank You for any help you can give!! Please let me know if their is any other information you need.

//This program calculates the gross pay for a salesperson

 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have removed some of the multiple spaces from your post, and borken about three lines into two lines each, because the lines would have been too long. I haven't done in very neatly, but you can see how you can break a line neatly.
I could see a few things I wasn't happy about. You appear to have get methods which ought to return a number, but they are returning Strings. A method returning a salary ought to return a number of some sort, not a String. The ideal number for money is BigDecimal, which you will find discussion about here, but you might think that is beyond the scope of your current app. You do realise that a Scanner can be made to return many different types of input, including different types of number. Go through its methods, and also look at the beginning of that documentation page, and you will find something useful.

Why are all your methods static?

Which loop is it you wish to terminate? I can only see loops which request repeat input. Doesn't the app terminate anyway?

 
Gess Bennett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritchie,

Thank you for the warm welcome, and thank you for cleaning up post for me. I can definitely see how it is more readable now.

The reason why I chose to pass a String first (I convert them at the end of the main method) is because my teachers directions were "create a user defined method named isNumeric, that will test to be sure the String value passed to it, is a number, the method will return a true if the String value passed to it, is a number and false if it is not a number", for all three of the number values. This is what I thought she meant by that

I have to have the isNumeric method, the calculateGrossPay method, and void printOutput method
Also, I need to use the String.format or System.out.printf to format the output.
I don't have to have all of the Other getMethod I used to prompt for each value, but I thought that was the right decision

The reason all of my methods are static is because without the static, I keep getting a compiler error which says " cannot make a static reference to the non-static method" for the calls in the main method if I don't have it there.

The loops repeat themselves if data is entered incorrectly, but once I finish inputting data it does not go back to the beginning of the loop, and my instructions were to have the loop continuously until the user chose to end it. I need another while loop that will make that happen, but no matter what I can't find the right place to put it.

Is it possible I can make it so that if the user enters a value (such as Done, or -999) at any time the program will terminate? Or would it be better to make it so the user can only terminate on one of the inputs.

I really am totally lost. I usually am very good at learning these things for myself, but I guess I've met my match with the while conditional.

Thank you for all of the pages, I am reviewing them now.


 
Gess Bennett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay how about this?



(I'm really trying to make the code look neat, I promise!!)

Does this qualify as a sentinel loop? Basically I'm trying to make it so that if the user enters "Q Q" or "q q" for their main
Problem is at the beginning of the next iteration, the loop starts at getBaseSalary, instead of at getSalespersonName.

Is there any way to fix that or do I have it completely wrong?

I know there are a lot of errors in the code in regards to my coding, but the while loop is my main concern right now because
of my deadline.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have errors, you should sort them out before adding any new features. One of the more prevalent beginners' mistakes we see is trying to do too much all at once and then getting confused.

Your code looks like code where you put too much in the main method, rather than creating an Employee object. Then you end up with the compiler complaining. There are good reasons for making things static, but static means not belonging to the object, so it is the exception. If I ever see that sort of compiler error I interpret it as
You have made too much static, now you are stuck with it.


I think, actually, there is good reason to make keyboard entry methods static, but they should be in their own class. You do not need to check whether input returned from a Scanner is numeric or not. The Scanner has methods which can do that check for you. Try them out, and you can create a static method which keeps prompting for a number until it gets one. I think you will take about 2 minutes to do that, because the logic is more‑or‑less the same as you are already using.

You appear to understand the logic of sentinel‑controlled iteration. But don't use System.exit(), which can be a bit vicious and can terminate other threads while they haven't finished their tasks. You need to learn some strange‑loooking syntax which I shall show you because otherwise you wouldn't guess it in a million years. What you do is get the entry in the () after while. You have to assign it before using it, because = has the lowest precedence of any operator, hence the additional (). And you will doubtless see my deliberate error within 30″There is no need for any exit code; that will terminate the loop when you enter qqq qqq and control passes to the end of the main() method and the app terminates and the JVM turns off and returns the memory to the operating system.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!