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.

11 Visitor Messages

  1. View Conversation
    no problem
  2. Ok great! thanks a lot man you were big help. Thanks for helping me out, im starting to get it a lot better now. I will see you in class tomorrow.

    Again thanks
  3. View Conversation
    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);
  4. ok i have my variable declared. Adding it to the message format, would it look like this?

    message += String.format( "%s $%.2f\n", name, charge, totalcharge);

    I am also not understanding where to add it inside the loop to add all 3 totals
  5. View Conversation
    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.
  6. alright cool and how would part four work out?
  7. View Conversation
    Yea, u just add the if statement above the message format.
  8. Thanks a lot, it is working now. I know for part 3 you have to add an if statement inside the while loop. I am not sure exactly how to do that though.
    I really appreciate your help
  9. View Conversation
    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);

    counter++;
    input.nextLine();


    }

    JOptionPane.showMessageDialog (null, message );

    }

    }
  10. Ok i tried that but it is now giving me an error. Here is what my code looks like for part 2.

    public class Electric2
    {
    public static void main( String args[])
    {

    Scanner input = new Scanner( System.in );

    double kilowatts=0;
    String name;
    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;


    message += String.format( "%s $%.2f", \n, name, charge);


    counter++;


    input.nextLine();


    }

    JOptionPane.showMessageDialog (null, message );

    }

    }
Showing Visitor Messages 1 to 10 of 11
Page 1 of 2 12 LastLast
Page 1 of 2 12 LastLast
About cb5950

Basic Information

Statistics


Total Posts
Total Posts
5
Posts Per Day
0.00
Visitor Messages
Total Messages
11
Most Recent Message
February 10th, 2011 12:35 AM
Total Thanks
Total Thanks
3
  • Thanked 0 Times in 0 Posts
General Information
Last Activity
May 5th, 2011 02:26 AM
Join Date
February 9th, 2011

2 Friends

  1. anthony23415 anthony23415 is offline

    Junior Member

    anthony23415
  2. JavaPF JavaPF is offline

    mmm.. coffee

    JavaPF
Showing Friends 1 to 2 of 2