Help with Loop and JOptionPane

Hey all,

I'm trying to run this program and eventually display three names and three total amounts in the same window; each on a new line.

My first issue is that I can't enter the second and third name because once the loop starts it skips over the name input part.

Second issue: How do I get the window to show up last with all three names and totals?
[code]
public class Electric2
{
public static void main(String[] Args)
{

Scanner input = new Scanner(System.in);

String name;
int totalCounter;
int kilowatts;
double total;

totalCounter=0;

while (totalCounter < 3)
{
System.out.println( "Enter customer name: ");
name = input.nextLine();

System.out.println( "Enter the amount of kilowatts used: " );
kilowatts = input.nextInt();
total = kilowatts * .056;
totalCounter++;



String message =
String.format(name+" $%.2f",total);

JOptionPane.showMessageDialog( null, message);
}
}
}
[/code]
Thanks to anyone who helps or takes the time to read,

Comments

  • Hey,
    Changes shown with via comments
    [code]
    import java.util.Scanner;
    import javax.swing.JOptionPane;


    public class Electric2 {

    public static void main(String[] Args) {

    Scanner input = new Scanner(System.in);

    String name;
    int totalCounter;
    int kilowatts;
    double total;

    totalCounter = 0;
    String message = ""; // CHANGED

    while (totalCounter < 3) {
    System.out.println("Enter customer name: ");
    // CHANGED name = input.nextLine();
    name = input.next();

    System.out.println("Enter the amount of kilowatts used: ");
    kilowatts = input.nextInt();
    total = kilowatts * .056;
    totalCounter++;

    message = message + String.format(name + " $%.2f
    ", total); // CHANGED
    } // MOVED above "output"
    JOptionPane.showMessageDialog(null, message);
    }
    }
    [/code]

    First needed to "accumulate" and save the input.
    Then display the information outside of loop.

    regards, se52
  • Thanks for the help
  • Better solution, will take a multi-part name:
    [code]
    import java.util.Scanner;
    import javax.swing.JOptionPane;


    public class Electric2 {

    public static void main(String[] Args) {

    Scanner input = new Scanner(System.in);

    String name;
    int totalCounter;
    int kilowatts;
    double total;

    totalCounter = 0;
    String message = ""; // CHANGED

    while (totalCounter < 3) {
    System.out.println("Enter customer name: ");
    name = input.nextLine();

    System.out.println("Enter the amount of kilowatts used: ");
    String kwString = input.nextLine(); // CHANGE
    kilowatts = Integer.parseInt(kwString); // CHANGE
    total = kilowatts * .056;
    totalCounter++;

    message = message + String.format(name + " $%.2f
    ", total); // CHANGED
    } // MOVED above "output"
    JOptionPane.showMessageDialog(null, message);
    }
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories