Results 1 to 9 of 9
  1. #1
    pwe9109 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Putting multiple outputs into one JOptionPane Dialog Box?

    I have a homework problem where you have to calculate the water bill for 3 customers while inputting their name and gallons used. Once you've inputted the name and charges for the 3 customers you have to print the output into one JOptionPane dialog box. I have some code, but I've hit a road block. Our teacher told us we have to use %.2f somehow, but I'm not quite sure. Can anyone help?

    Here's my code so far

    import java.util.Scanner;
    import javax.swing.JOptionPane;

    public class Utilities3
    {
    public static void main(String[]args)
    {
    Scanner input = new Scanner(System.in);
    String name;
    int gallons;
    double charges;
    double prsewercharge = 50;
    double sewercharge = 25.50;
    int counter;
    String output;

    counter = 1;

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

    System.out.println( "Enter gallons used");
    gallons = input.nextInt();

    if ((gallons *.036) > 100)
    charges = (gallons * .036) + prsewercharge;
    else
    charges = (gallons * .036) + sewercharge;

    output += name + " $" + charges + "\n";

    counter = counter + 1;

    }

    JOptionPane.showMessageDialog(null, output);

    input.nextLine();

    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Putting multiple outputs into one JOptionPane Dialog Box?

    use %.2f somehow, but I'm not quite sure
    Look at the String format method for how to use that format String.

  3. #3
    pwe9109 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Putting multiple outputs into one JOptionPane Dialog Box?

    I think I may be getting closer, but still getting error messages

    import java.util.Scanner;
    import javax.swing.JOptionPane;

    public class Utilities3
    {
    public static void main(String[]args)
    {
    Scanner input = new Scanner(System.in);
    String name;
    int gallons;
    double charges;
    double prsewercharge = 50;
    double sewercharge = 25.50;
    int counter;

    counter = 1;

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

    System.out.println( "Enter gallons used");
    gallons = input.nextInt();

    if ((gallons *.036) > 100)
    charges = (gallons * .036) + prsewercharge;
    else
    charges = (gallons * .036) + sewercharge;

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

    counter = counter + 1;

    input.nextLine();

    }

    JOptionPane.showMessageDialog(null, message);

    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Putting multiple outputs into one JOptionPane Dialog Box?

    Quote Originally Posted by pwe9109 View Post
    I think I may be getting closer, but still getting error messages
    What error messages? Please show the entire message, not your interpretation of it. Also, indicate which line(s) is causing the error.

  5. #5
    pwe9109 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Putting multiple outputs into one JOptionPane Dialog Box?

    Utilities3.java:42: cannot find symbol
    symbol : variable message
    location: class Utilities3
    JOptionPane.showMessageDialog(null, message);
    and it points to message in the last line with a ^

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Putting multiple outputs into one JOptionPane Dialog Box?

    It's telling you that the variable message does not exist in the scope of where you're trying to use it. Likely this is due to your having declared the variable from within a while block -- and it is visible only from within the scope from which it was declared in, from within the while block -- and then tried to use it outside of the while block.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Putting multiple outputs into one JOptionPane Dialog Box?

    The definition of the variable message can't be seen where you are trying to reference it. You need to move the variable's definition to the same scope level (within the same pair of {}s) as where you are trying to reference it.

  8. #8
    pwe9109 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Putting multiple outputs into one JOptionPane Dialog Box?

    Ok, I'm having trouble on where and how to declare the variable within my code. Do I need to add "String message" before the while loop? Or put it inside of it?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Putting multiple outputs into one JOptionPane Dialog Box?

    Put the definition where it is visible to all the code that needs to see it. If you put it inside of a pair of {}s then you can not use it outside of the {}s.

Similar Threads

  1. Default Focus in JOptionPane Dialog
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 5
    Last Post: 11-11-2009, 02:59 PM
  2. Default Focus in JOptionPane Dialog
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 1
    Last Post: 11-09-2009, 12:15 PM
  3. JOptionPane dialog (Localizing)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 11:36 AM
  4. JOptionPane - message dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:11 AM
  5. JOptionPane - input dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •