Hi, I'm having a problem with a code which is supposed to read in values for the number of boxes of cookies sold by girl scouts and compute the total money collected for each troop. It must work for more than just a few troops, so I have a while loop, but I can't get the program to allow the troop name to be entered after the first run through.

Cookie Sales Report for Young Scouts of America
Enter the troop name (or exit to quit):
Tulips
Enter number of boxes of Chocolate Mint Wafers:
1
Enter number of boxes of Peanut Delights:
2
Enter number of boxes of Coconut Plops:
3
The total collected by Troop Tulips is $16.75
Enter the troop name (or exit to quit):
Enter number of boxes of Chocolate Mint Wafers:

As you can see, the change of the String t is skipped. An additional troop cannot be named and the loop can't be exited.
I may just be approaching this all wrong, but either way, I would greatly appreciate any help.

Also, once I figure this out, I need to print information for each troop. I have no idea how I'm going to accomplish that without setting a limit on the number of troops that can be entered and assigning each to a variable, so a hint on that would be great, too.

Thanks!

October 9th, 2012, 07:22 PM

curmudgeon

Re: While loop won't allow variable to change

That code that is inside of the while loop block is the code that will be repeated. Now look to see where you ask for the troop name? Is it inside of the while loop block or before it?

October 9th, 2012, 07:27 PM

Thor

Re: While loop won't allow variable to change

Both. I ask for the name before/outside of the loop to provide a condition, then at the end of the loop, ask for the name again in order to provide a chance for the condition to change. If the name is not "exit" I want the loop to run again with the new name entered, but if "exit" is entered as the new name, the loop should end.

As far as printing information for each troop, are you supposed to print as you go, or store the information and print later on? This makes a big difference on how to handle the solution. Either way the requirement goes, there are ways to store an unknown number of entries as the program runs.

October 9th, 2012, 07:43 PM

Thor

Re: While loop won't allow variable to change

I'll need to print the total and average sale of the troops overall and then the troop which sold the least and the troop which sold the most. This is all printed after input is finished, so I guess I need to store the information. The only thing I need to print as I go is the total collected by each troop individually.

Thanks for the tutorial. I understand how loops work (at least I thought I did). The reason I'm so confused by this is because I've done things like it in the past and never had a problem.

October 9th, 2012, 07:51 PM

jps

Re: While loop won't allow variable to change

I am guessing you are taking a class? Are there any specifications on how to handle the input until the end? If you have questions about an assignment it is useful to post the instructions with the code and question.

October 9th, 2012, 07:58 PM

curmudgeon

Re: While loop won't allow variable to change

One thing you should consider, after each in.nextInt(); call in.nextLine(); to swallow the end of line token. This will prevent the Scanner object from swallowing it when you don't want it to, like at the end of your while loop. So:

This is for my programming class, yes. I edited my first post to include instructions.

October 9th, 2012, 08:13 PM

jps

Re: While loop won't allow variable to change

The instructions do not say exactly how to do it, so see what you can come up with and ask questions if you get stuck. If you are having a hard time getting started on it, think about how you would do it if your job was to walk around with a pencil and paper, and calculate the totals yourself.

October 9th, 2012, 08:16 PM

Thor

Re: While loop won't allow variable to change

curmudgeon, that didn't do anything for me, I just got errors. Thanks for the idea!

October 9th, 2012, 08:18 PM

Thor

Re: While loop won't allow variable to change

jps, I think I would be fine if I could just get the while loop to work. The logic isn't complicated, but it won't function at all if I can't get information entered for more than one troop.

October 9th, 2012, 08:19 PM

curmudgeon

Re: While loop won't allow variable to change

Quote:

Originally Posted by Thor

curmudgeon, that didn't do anything for me, I just got errors. Thanks for the idea!

What errors?

October 9th, 2012, 08:27 PM

Thor

Re: While loop won't allow variable to change

Honestly I have no idea, but they're gone now. I put everything in and it didn't work, so I posted that reply, but just now I put everything back in to see what the error message was and it works fine.