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.


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

Conversation Between cb5950 and anthony23415

6 Visitor Messages

  1. no problem
  2. The variable should be declared like this
    double totalcharge = 0;

    Then inside the while loop, you can add it above the message format or under the if statement. This will add all the total.
    totalcharge += total;

    Also leave the message format in the while loop alone. Just keep it like this.
    message = String.format("%s $%.02f",name,total);

    Now above the JOptionPane this is the format for the Total Charge to show.
    message = message + String.format("Total Charge $%.02f",totalcharge);
  3. You just add another double variable. You can call it what ever you want. I called mine totalcharge. Then u add that variable inside the while loop to add all 3 totals. Then above the JOptionPane, you have to add another message format.
  4. Yea, u just add the if statement above the message format.
  5. import java.util.Scanner;
    import javax.swing.JOptionPane;

    public class bored
    public static void main( String args[])

    Scanner input = new Scanner( System.in );

    double kilowatts=0;
    String name;
    String message needs to have this since JOptionPane wont see it initialized.
    String message = "";
    double charge=0;

    int counter=1;

    while( counter <= 3)

    System.out.println( "Enter name: ");
    name = input.nextLine();

    System.out.println( "Enter kilowatts: ");
    kilowatts = input.nextInt();

    charge = kilowatts * .056;

    The \n has to be inside the quotations.
    message += String.format( "%s $%.2f \n", name, charge);



    JOptionPane.showMessageDialog (null, message );


  6. Alright just got off work. Now if u still need the 2nd part of the homework about showing each name in one list.

    Here is the part in the first one where u have a format.
    message = String.format("%s $%.02f,name,total);

    In the 2nd part all you do is add a + sign in front of the equal sign. And \n which is adding a new line for each name.
    message += String.format("%s $%.02f\n,name,total);

    Now the take the JOptionPane.showMessageDialog(null,message);, and move it outside the while loop. And thats the 2nd one.

    If u rather have me email u on your gmail account instead of this site, I can as well.
Showing Visitor Messages 1 to 6 of 6