Results 1 to 9 of 9
- 09-12-2011, 03:21 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
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();
}
}
- 09-12-2011, 03:45 AM #2
Re: Putting multiple outputs into one JOptionPane Dialog Box?
Look at the String format method for how to use that format String.use %.2f somehow, but I'm not quite sure
- 09-12-2011, 03:54 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
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);
}
}
-
Re: Putting multiple outputs into one JOptionPane Dialog Box?
- 09-12-2011, 04:00 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
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 ^
-
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.
- 09-12-2011, 04:11 AM #7
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.
- 09-12-2011, 04:29 AM #8
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
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?
- 09-12-2011, 01:43 PM #9
Similar Threads
-
Default Focus in JOptionPane Dialog
By Gajesh Tripathi in forum AWT / SwingReplies: 5Last Post: 11-11-2009, 02:59 PM -
Default Focus in JOptionPane Dialog
By Gajesh Tripathi in forum AWT / SwingReplies: 1Last Post: 11-09-2009, 12:15 PM -
JOptionPane dialog (Localizing)
By Java Tip in forum Java TipReplies: 0Last Post: 03-14-2008, 11:36 AM -
JOptionPane - message dialog
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:11 AM -
JOptionPane - input dialog
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks