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 10 of 10

Thread: How to make for loop work?

  1. #1
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to make for loop work?

    Actually the for loop works it's just that what I am having to do is pushed through with JOptionPane output. It will only print out the last part of the loop when it needs to print all the runs. This is what I have:

    //
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
    public class ApplianceTest4
    {
     
    	public static void main(String args[])
    	{
    		Scanner input = new Scanner(System.in);
     
    		String repeat;
    		int counter;
    		String choice;		
    		double purchase1;
    		double purchase2;
    		String message="";
    		int chosen;
    		String amount;
    		int x;
    		String amount2;
    		int y;
    		int i;
     
    		Appliance app1 = new Appliance("washer", "Kenmore", 102956, 299.99);
    		Appliance app2 = new Appliance("stove", "GE", 8956, 575.75);
     
    		repeat = JOptionPane.showInputDialog("Please enter how many purchese are going to be made:\n");
    			counter = Integer.parseInt(repeat);
    		for(i = 1; i <= counter; i++)
    		{
     
    			choice = JOptionPane.showInputDialog("Type 1 if purchasing washer or 2 for stove: ");
    				chosen = Integer.parseInt(choice);		
     
    			switch(chosen)
    			{
    				case 1: {
    					amount = JOptionPane.showInputDialog("Enter the amount of washers:\n");
    						x = Integer.parseInt(amount);
    					purchase1 = app1.getPrice() * x;
    					message = String.format("You purchased %s washers for $%.2f.\n", x, 			
     
    					purchase1);
    					break;
    				}
    				case 2:
    					{amount2 = JOptionPane.showInputDialog("Enter the amount of stoves:"); 
    						y = Integer.parseInt(amount2);
    					purchase2 = app2.getPrice() * y;
    					message = String.format("You purchased %s stoves for $%.2f.\n", y, 			
     
    					purchase2);
    					break;}
    				default:
    					{message = String.format("Incorrect choice choose 1 or 2");
    					break;}
     
    			}
     
     
    		}
     
    		JOptionPane.showMessageDialog(null, message + i);
     
     
     
    	}
    }


    Hopefully somebody can show me where I went wrong.
    Last edited by Dragonkndr712; March 8th, 2011 at 02:01 AM. Reason: Trying to get it in the proper format.


  2. #2
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make for loop work?

    I want to apologize now I am unsure what I did wrong when trying to put it in the proper format.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to make for loop work?

    The formatter's picky about spaces.

    So [highlight=java] (no spaces) is the correct syntax, where-as [highlight = java] (with spaces) won't work properly.

    I'm a little unsure of what you mean by printing out only the last part of the loop, could you perhaps provide sample inputs and outputs?

  4. #4
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make for loop work?

    When the loop runs it wants to know how many times to repeat. It does this just fine but when it comes to the last runthrough of the loop where it should print out all of the messages it only prints the last time through the loop.

    For instance, you say 3 times through it should print out three messages in one JOptionPane dialog box. But it only prints out one message.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to make for loop work?

    The problem is you're re-assigning the value of message to a new string, rather than simply appending to it. This erases all the old messages.

  6. #6
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make for loop work?

    Ok, how do I fix that?

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to make for loop work?

    message += "your message goes here";

    An alternative (if you're interested in performance, though with modern day computers and this situation this is a non-issue) is to use the StringBuilder class since it is designed to work with mutable strings (appending, changing, etc.).

    StringBuilder message = new StringBuilder();
    message.append("your message goes here");

  8. #8
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make for loop work?

    I'm sorry I am a newbie and I do not understand what this does.

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to make for loop work?

    In both sets of code it tells you how to append to a string. For String objects, the + operator is defined as appending two strings together (the += operator similarly appends and re-assigns). Note that with string objects, you must re-assign the results back into a string since Strings are immutable (i.e. can't ever be changed), so the result of the appending is a new string.

    In you code above, you have code that works something like this:
    message = "";
    message = "this is message 1\n";
    message = "this is message 2\n";
    message = "this is message 3\n";

    However, this is re-assigning a completely new string to message rather than appending it. Rather, you need to be appending to the message string.
    message = "";
    message += "this is message 1\n";
    message += "this is message 2\n";
    message += "this is message 3\n";

    I'll leave fixing your code to implement this up to you.

  10. #10
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make for loop work?

    thank you that did it.

Similar Threads

  1. I can't get this loop to work correctly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 27th, 2011, 04:20 PM
  2. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 02:58 PM
  3. Unnable to make validations work for login screen
    By fierof2 in forum Web Frameworks
    Replies: 8
    Last Post: July 29th, 2010, 07:12 AM
  4. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 10:06 PM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM