获取错误线程“main”中的异常java.util.IllegalFormatConversionException:d!= java.lang.String

时间:2017-05-05 06:02:56

标签: java

我遇到了这个错误“线程中的异常”主“java.util.IllegalFormatConversionException:d!= java.lang.String”。我发现这是Java新手中一个相当普遍的问题,虽然我试图将建议应用到我的代码中,但我没有取得任何成功。我希望得到一些反馈和建议,希望如何正常运行。我能够使用println使其正常运行,但输出格式化是分配所必需的。任何意见,将不胜感激。 谢谢。

  import javax.swing.JOptionPane;

 public class UserIO {
 public static void main(String[] args) {

    // initialize coefficients.
            double a;
            double b;
            double c;
            // int counter = 0;

            String userInput; // take a String for input.

            // display message. returns null.
            JOptionPane.showMessageDialog(null,
                    "Welcome. Input positive a real number for a, b, and c. Numbers must range between 1.0 -10.0");
            userInput = JOptionPane.showInputDialog("Input a real number for a.");
            a = Double.parseDouble(userInput);// convert String userInput to real
                                                // numbers.
            System.out.println("Number for a = " + userInput); // print a

            userInput = JOptionPane.showInputDialog("Input a real number for b. ");
            b = Double.parseDouble(userInput);
            System.out.println("Number for b = " + b); // print b

            userInput = JOptionPane.showInputDialog("Input a real number for c.");
            c = Double.parseDouble(userInput);
            System.out.println("Number for c = " + c); // print c

            // calculate quadratic equation 5 times, store in xValues then, print to
            // screen.

            double product;
            double[] xValues = new double[5]; // array index of 5.

            for (int i = 4; i >= 0; i--) {

                xValues[i] = i + 1; // fills array with numbers 1-5.
                // raise x to the i'th degree.
                product = a * Math.pow(xValues[i], 2) + b * xValues[i] + c;

                // System.out.println("[" + i + "]"+ " " + xValues[i] + " " +// product);
                System.out.printf("%d , i  " + "%1.2f ", xValues[i] + " " + "%1.3f ", product);

            } // end loop

}

}

2 个答案:

答案 0 :(得分:0)

我想这一行会导致问题:

System.out.printf("%d , i  " + "%1.2f ", xValues[i] + " " + "%1.3f ", product);

可以简化为:

System.out.printf("%d , i  %1.2f ", xValues[i] + " %1.3f ", product);

您可以清楚地看到,您尝试将%d替换为字符串xValues[i] + " %1.3f "

我认为你的意思不同。

答案 1 :(得分:0)

查看API,它应如下所示。

System.out.printf("%d , %1.2f , %1.3f ", i, xValues[i], product);