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

Thread: Need help with while loop

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Need help with while loop

    This code will print out 1 user and total one at a time in a dialog box 3 times. Now the code is how is suppose to be. Now how would I use this same code to show in the same dialog box but all 3 names and total in one dialog box?

    Here is a quick output of how it should be. But not sure how to do it.

    Thanks



    mport javax.swing.JOptionPane;
    public class testing {
     
    	public static void main(String[] args){
     
    		Scanner input = new Scanner(System.in);
     
    		String name;
    		int kilowattsUsed;
    		double total;
     
    		int counter = 1;
    		while (counter < 4){
    			System.out.println("Enter name.");
    			name = input.next();
    			System.out.println("How much kilowatts used?");
    			kilowattsUsed = input.nextInt();
     
    			total = kilowattsUsed *.056;
    			counter = counter + 1;
     
    			String message = String.format("%s $%.02f",name,total);
                            JOptionPane.showMessageDialog(null,message);
     
    		}	
    	}
    }
    Last edited by anthony23415; February 6th, 2011 at 01:02 PM. Reason: put code with highlight tags


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Need help with while loop

    import javax.swing.JOptionPane;
    import java.util.Scanner;
     
    public class testing {
     
        public static void main(String[] args){
     
            Scanner input = new Scanner(System.in);
     
            String name = "";
            int kilowattsUsed = 0;
            double total = 0;
           String message = "";
     
            int counter = 1;
            while (counter < 4){
                System.out.println("Enter name.");
                name = input.next();
                System.out.println("How much kilowatts used?");
                kilowattsUsed = input.nextInt();
     
     
                total = kilowattsUsed *.056;
                counter = counter + 1;
               if (counter ==2)
    {
                message = String.format("%s $%.02f",name,total);
    }
     
    else
    {
    message = message + "\n" + String.format("%s $%.02f",name,total);
    }
     
     
            }  
    JOptionPane.showMessageDialog(null,message, "Message", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    The "\n" causes it to skip a line so they aren't on the same line.
    The if and else make sure that there isn't an extra line made before the first user is entered
    The output is put after while loop so it will show only once instead of 3 times.
    message is defined and initialized before while loop so that it won't claim it can't find symbol or that it wasn't initialized after while loop exits.

    Ok...I tested it and it works.
    Last edited by javapenguin; February 6th, 2011 at 03:39 PM.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    anthony23415 (February 6th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need help with while loop

    wow it worked, thanks a bunch javapenguin

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM