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();
}
}
Re: Putting multiple outputs into one JOptionPane Dialog Box?
Quote:
use %.2f somehow, but I'm not quite sure
Look at the String format method for how to use that format String.
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?
Quote:
Originally Posted by
pwe9109
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.
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.
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.
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?
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.