线程“main”中的异常java.lang.NumberFormatException:empty String FloatingDecimal& parseDouble

时间:2016-09-18 17:03:47

标签: java java.util.scanner joptionpane decimalformat currency-formatting

我收到了错误消息。出于某种原因,编译代码时错误没有出现:

//这是我得到的错误     线程“main”中的异常java.lang.NumberFormatException:empty String     at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)     at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)     在java.lang.Double.parseDouble(Double.java:538)     在Project3A.main(Project3A.java:42)

    //Code starts here
    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));
      System.out.println(df.format(d));
      }
}

1 个答案:

答案 0 :(得分:0)

这不是有效的Java:

JOptionPane.showInputDialog,"Enter number of apples";

调用任何方法时,必须在括号内包含适当的参数,例如

myMethodCall(1, 2, 3);

您的通话不使用括号,也没有意义。阅读JOptionPane API以查看您可能需要的参数。