Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: While loop won't allow variable to change

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default While loop won't allow variable to change

    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.

    Here is my code:
    package lab5;
     
    import java.util.Scanner;
     
    public class Scouts {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Scanner in = new Scanner (System.in);
     
    		int w = 0;
    		int d = 0;
    		int p = 0;
    		double total = 0;
     
    		System.out.println("Cookie Sales Report for Young Scouts of America");
    		System.out.println("Enter the troop name (or exit to quit): ");
    		String t = in.nextLine();
     
    		while (!t.equals("exit"))
    			{
    			System.out.println("Enter number of boxes of Chocolate Mint Wafers: ");
    			w = in.nextInt();
    			System.out.println("Enter number of boxes of Peanut Delights: ");
    			d = in.nextInt();
    			System.out.println("Enter number of boxes of Coconut Plops: ");
    			p = in.nextInt();
    			total = w*3+d*3.5+p*2.25;
    			System.out.println("The total collected by Troop " + t + " is $" + total);
    			System.out.println("Enter the troop name (or exit to quit): ");
    			t = in.nextLine();
    			}
    		System.out.println("The total collected overall is $" + total + ".");
     
    	}
     
    }

    This is what I get when I run the program:

    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!
    Last edited by Thor; October 9th, 2012 at 09:50 PM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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?

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: While loop won't allow variable to change

    Hi Thor and welcome to the forum.

    Suggested Reading

    //some block of code starts
    {
       //some code may do some work here... maybe not...
     
       while(true) {
          myVariable = getValidValueFromUser();
          if(myVariable == valueToQuit) {
             break;
          }
          //else {
             processUserInput();
             processSomeMore();
             computersAreFastSoDoEvenMore();
             displayResults();
          //}
       }
     
       //some code may do some work here... maybe not...
    }

    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.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  7. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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:

    while (!t.equals("exit")) {
      System.out.println("Enter number of boxes of Chocolate Mint Wafers: ");
      w = in.nextInt();
      in.nextLine();
      System.out.println("Enter number of boxes of Peanut Delights: ");
      d = in.nextInt();
      in.nextLine();
      System.out.println("Enter number of boxes of Coconut Plops: ");
      p = in.nextInt();
      in.nextLine();
      total = w*3+d*3.5+p*2.25;
      System.out.println("The total collected by Troop " + t + " is $" + total);
      System.out.println("Enter the troop name (or exit to quit): ");
      t = in.nextLine();
    }
    System.out.println("The total collected overall is $" + total + ".");

  8. The Following User Says Thank You to curmudgeon For This Useful Post:

    Thor (October 9th, 2012)

  9. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop won't allow variable to change

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

  10. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  11. #10
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!

  12. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  13. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: While loop won't allow variable to change

    Quote Originally Posted by Thor View Post
    curmudgeon, that didn't do anything for me, I just got errors. Thanks for the idea!
    What errors?

  14. #13
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    Maybe I put it in wrong before, either way, thanks! it works.

  15. #14
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: While loop won't allow variable to change

    Glad you've got it working!

Similar Threads

  1. write the program in C, C++, Java or C#. Change variable types accordingly.
    By indrasensingh01 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2012, 06:34 PM
  2. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  3. Setting variable correctly in sentinel-controlled loop
    By exchaoordo in forum Loops & Control Statements
    Replies: 1
    Last Post: June 1st, 2012, 03:40 PM
  4. [SOLVED] Assign a loop to variable to compare strings
    By norske_lab in forum Loops & Control Statements
    Replies: 3
    Last Post: March 6th, 2012, 02:46 PM
  5. Variable updates after loop
    By tecno40 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 9th, 2011, 05:34 AM