【问题标题】:How do I resolve this java.lang.NumberFormatException? [duplicate]如何解决这个 java.lang.NumberFormatException? [复制]
【发布时间】:2017-02-21 23:27:36
【问题描述】:

请看下面我的代码:

如何解决我从 eclipse 收到的错误?

package finalExam;

//this is required for JOptionPane to work
import javax.swing.JOptionPane; 

//this allows for exception throwing
import java.io.*;

public class Geometry {

    public static void main(String[] args) throws IOException {

boolean valid = false;

int menuChoice;

do {
        // create a menu and display it to the user
        // then ask the user to choose an option
        String menu = "1) Calculate the area of a circle\n"
                    + "2) Calculate the area of a rectangle\n"
                    + "3) Calculate the area of a triangle\n"
                    + "4) Quit\n"
                    + "Please enter your choice: (1, 2, 3, or 4)";

        JOptionPane.showInputDialog(menu);
        menuChoice = Integer.parseInt(menu);

        if(menuChoice == 1)
        {
            String unknownRadius = JOptionPane.showInputDialog("What is the radius of the circle?");
            double knownRadius = Double.parseDouble(unknownRadius);
            double circleArea = Math.pow(knownRadius, 2) * 3.14159;
            JOptionPane.showMessageDialog(null, "The area of the circle is " + circleArea);
            valid = true;

        } else if(menuChoice == 2){
            String unknownLength = JOptionPane.showInputDialog("What is the length of the rectangle?");
            double knownLength = Double.parseDouble(unknownLength);
            String unknownWidth = JOptionPane.showInputDialog("What is the width of the rectangle?");
            double knownWidth = Double.parseDouble(unknownWidth);
            double rectangleArea = knownLength * knownWidth;
            JOptionPane.showMessageDialog(null, "The area of the rectangle is " + rectangleArea);
            valid = true;

        } else if(menuChoice == 3){
            String unknownBase = JOptionPane.showInputDialog("What is the base length of the triangle?");
            double knownBase = Double.parseDouble(unknownBase);
            String unknownHeight = JOptionPane.showInputDialog("What is the height of the triangle?");
            double knownHeight = Double.parseDouble(unknownHeight);
            double triangleArea = (knownBase / 2) * knownHeight;
            JOptionPane.showMessageDialog(null, "The area of the triangle is " + triangleArea);
            valid = true;

        }else if(menuChoice == 4){
            System.exit(0);

        }else if(menuChoice > 0)
            JOptionPane.showMessageDialog(null, "Please enter positive numbers only!");
}

while(!valid || menuChoice != 4);

}
}

这是错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "1) Calculate the area of a circle
2) Calculate the area of a rectangle
3) Calculate the area of a triangle
4) Quit
Please enter your choice: (1, 2, 3, or 4)"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at finalExam.Geometry.main(Geometry.java:25)

【问题讨论】:

  • 您试图将menu 解析为整数?您希望它如何工作?
  • 我认为您必须学习一些有关 Java 和 Swing 的基础知识。这是example for JOptionPane

标签: java eclipse numberformatexception


【解决方案1】:

您错过了在字符串中捕获选项响应:

   String choice = JOptionPane.showInputDialog(menu);
   menuChoice = Integer.parseInt(choice);

或者直接解析:

 menuChoice = Integer.parseInt(JOptionPane.showInputDialog(menu));

【讨论】:

    【解决方案2】:

    我相信你想要做的是:

    // create a menu and display it to the user
    // then ask the user to choose an option
    String menu = "1) Calculate the area of a circle\n"
         + "2) Calculate the area of a rectangle\n"
         + "3) Calculate the area of a triangle\n"
         + "4) Quit\n"
         + "Please enter your choice: (1, 2, 3, or 4)";
    /*JOptionPane#showInputDialog returns a string of the input, so that is what you want to parse*/
    /*also you may want to add a try catch to prevent crashes, should the user not input a valid integer*/
    menuChoice = Integer.parseInt(JOptionPane.showInputDialog(menu));
    
    if(menuChoice == 1)
    {
    

    您最初所做的是尝试从菜单文本中解析一个整数 [1) 计算面积...]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 2017-08-27
      • 2021-08-22
      • 1970-01-01
      相关资源
      最近更新 更多