Jasvant Singh Dosanjh Jasvant Singh Dosanjh - 1 year ago 68
Java Question

Error with JOptionPane.showInputDialog

I am having a hard time figuring out how to create an input dialog with JOptionPane, and taking the input data, and use that data to convert that data a numerical variable. Also, should i declare int numloaves, nummilk, lbscheese as doubles, with the "$" sign included with them, or should that be at the end? I am having errors with my import statements at the beginning, and with my "NumberFormat moneyFormat" line, and with my scanner sc code.

import java.util.scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.util.Locate;
import java.text.NumberFormat;

public class Project3A
public static void main(String[] args)
int numloaves = 0;
int nummilk = 0;
double lbscheese = 0.00;
double brdperloaf = 1.50;
double milkperqrt = 1.00;
double chseperlbs = 0.50;
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance(Locale.US);

Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of loaves of bread: ");
int numloaves = sc.nextInt();

Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of quarts of milk: ");
int nummilk = sc.nextInt();

Scanner sc = new Scanner(System.in);
System.ot.println("Enter the number of pounds of cheese: ");
int chseperlbs = sc.nextInt();

double brdcst = (numloaves * brdperloaves);
double mlkcst = (nummilk * milkperqrt);
double chsecst = (lbscheese * chseperlbs);
double ttlcost = (brdcst + mlkcst + chsecst);

String inputStr = JOptionPane.showInputDialog(null, "Enter number of apples");
double value = Double.parseDouble(" ");

System.out.println("Cost of Bread: " + brdcst);
System.out.println("Cost of Milk: " + mlkcst);
System.out.println("Cost of Cheese: " + chsecst);

double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
JOptionPane.showMessageDialog(null, "Total Cost of bread, milk and cheese is " + df.format(d));

Answer Source

This isn't valid Java:

JOptionPane.showInputDialog,"Enter number of apples";

When calling any method you must include the appropriate parameters within parenthesis, e.g.

myMethodCall(1, 2, 3);

Your call uses no parenthesis and doesn't make sense. Read the JOptionPane API to see what parameters you may need.