如何读取NumberFormatException错误消息?

时间:2014-08-03 09:07:15

标签: java swing debugging exception stack-trace

[注意] 添加到下面的答案,了解有关阅读堆栈跟踪的其他好建议

我在正在制作的程序中收到错误,但我不知道如何修复它。

用户必须从留言箱中选择选项,我必须使用用户输入来计算他们将拥有的学费及其折扣。

如果我跑了!

 run: Exception in thread "main" java.lang.NumberFormatException: For input string: ""
                at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
                at java.lang.Integer.parseInt(Integer.java:468)
                at java.lang.Integer.parseInt(Integer.java:497)
                at cabrera.Main.main(Main.java:26)
        Java Result: 1
        BUILD SUCCESSFUL (total time: 6 seconds)

代码

package cabrera;

/**
 *
 * @author Owner
 */
import javax.swing.JOptionPane;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String tuition1 = "", scholar1 = "";
        int tuition = 0, scholar = 0;
        double discount = 0.0;

        JOptionPane.showInputDialog("Input Your Tuition" + tuition1);
        JOptionPane.showInputDialog("Choose A Scholar Discount:" + "\n• 1 = 80% " + "\n• 2 = 50%" + "\n• 3 = 25%" + "\n• 4 = No Discount !" + scholar1);

        tuition = Integer.parseInt(tuition1);
        scholar = Integer.parseInt(scholar1);

        JOptionPane.showMessageDialog(null, "Your Total Tuition is: " + tuition);

        // If-elseif-else statements based on scholar variable

    }
}

如何修复此错误消息?

更多信息

我最近遇到了一个执行得很糟糕的question。糟糕的标题,糟糕的代码,糟糕的问题(实际上不是问题,但经过一番思考后可以假设)。它来自一个新的SO用户。

我想帮忙,看一下代码。所以我编辑了这篇文章;修改了代码布局,简化了代码,询问了OP想要问的问题等等......现在问题就在上面;然而,问题被搁置,我读到问题可能需要很长时间才能离开On Hold。你需要像5票一样的东西在它被搁置后重新开放。鉴于问题是第一次被问到已经有一段时间了,我看不到会发生的事情。

我已经完成了答案并且不想浪费一个好的答案

2 个答案:

答案 0 :(得分:21)

好的,让我们先看看如何找到问题!

(3) run: Exception in thread "main" java.lang.NumberFormatException: For input string: ""
               at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
               at java.lang.Integer.parseInt(Integer.java:468)
(2)            at java.lang.Integer.parseInt(Integer.java:497)
(1)            at cabrera.Main.main(Main.java:26)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 6 seconds)

看看......

  • (1),您可以在Main.java中看到line 26
  • 出现问题
  • (2),您可以在此处查看有关该问题的更多信息。事实上,您知道Integer.parseInt存在问题。

    • 您不必担心查看此代码,因为您在程序包Integer
    • 中没有名为parseInt的方法java.lang
    • 您只知道此处存在问题,它会处理您在Integer.parseInt代码中调用的Main.java

    • 因此我们找到了一个可能的区域来检查错误!

      tuition = Integer.parseInt(tuition1);    
      scholar = Integer.parseInt(scholar1);
      
  • (3),在这里我们可以看到NumberFormatException被抛出,因为你给它(Integer.parseInt)输入""(空字符串)


那么你如何解决它?

  1. 首先,您必须查看Integer.parseInt

    的输入
    • 您可以看到您通过了tuition1scholar1

  2. 查看更改/创建这两个变量的位置。

    • 你可以看到你做的最后一件事是创建这两个变量并为它们分配空字符串(""
    • 有问题!!

  3. 因此您需要为用户分配一个值。这将通过showInputDialog(...)完成。

    • showInputDialog(...) 会返回您可以使用的值! See here!
    • 你所要做的只是......

      tuition1 = JOptionPane.showInputDialog("Input Your Tuition");     
      scholar1 = JOptionPane.showInputDialog("Choose A Scholar Discount: 1... 2... 3... 4...")
      
    • 请注意,当您要为用户分配值时,tuition1scholar1不应放在对话框内。您需要让用户在对话框中输入一些文本,并在按下“确定”按钮时返回该值。因此,您必须将该返回值分配给tuition1scholar1

答案 1 :(得分:0)

您收到此异常是因为用户可以自由输入不是数字的输入。如果传递给此方法的参数不是有效数字,Integer.parseInt(String s)将抛出NumberFormatException。您可以通过使用正则表达式限制用户仅输入数字。代码如下:

String tuition1="";     
int tuition = 0;
boolean numFound =false;
try{
    while(!numFound){
        tuition1 = JOptionPane.showInputDialog("Input Your Tuition:");
        Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
        Matcher m = p.matcher(tuition1);
        if (m.find()) {
            JOptionPane.showMessageDialog(null, "Please enter only numbers");
        }
        else{       
            tuition = Integer.parseInt(tuition1);       
            numFound = true;
        }
    }
}
catch (Exception e) { 
    e.printStackTrace();
}

如果他尝试输入无法转换为整数的内容,则会向用户显示“请仅输入数字”的消息。