【问题标题】:my error is java.lang.NumberFormatException我的错误是 java.lang.NumberFormatException
【发布时间】:2014-11-18 02:34:45
【问题描述】:

我正在使用 jcreator。它表示该过程已完成,但是当您切换到 case 4 然后选择数字 1-9 时,它表示 java.numberformatexception。是什么原因造成的,我该如何解决?我只是这里的初学者。

import javax.swing.JOptionPane;

public class Exer29A {

    public static String inputdialog(String s)
    {
        JOptionPane.showInputDialog(null,s);
        return s;
    }   
    static void messagedialog(String r) 
    {
        JOptionPane.showMessageDialog(null, r);
    }
    public static void main(String[] args) {
        int xx, x1, x2;
        String n1, n2, LName, MName, FName;
        FName = JOptionPane.showInputDialog(null, "What's your first name?");
        MName = JOptionPane.showInputDialog(null, "What's your Middle name?");
        LName = JOptionPane.showInputDialog(null, "What's your Last name?");
        n2 = JOptionPane.showInputDialog(null, "So you are " + FName.substring(0,1).toUpperCase() + FName.substring(1).toLowerCase() + " " + MName.substring(0,1).toUpperCase() + MName.substring(1).toLowerCase() + " " + LName.substring(0,1).toUpperCase() + LName.substring(1).toLowerCase() + " " + "Do you wish to continue?[Y/N]");

        if (n2.equalsIgnoreCase("Y"))
            {
                 x1 = Integer.parseInt(JOptionPane.showInputDialog(null, "What do you want to do? \n 1. Add 2 integers \n 2. Area of a rectangle \n 3. Area of a triangle \n 4. Know your fate.")); 
                    switch (x1)
                    {
                    case 1:
                    {
                        x1 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1st integer."));
                        x2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 2nd integer"));
                        messagedialog("The sum of two numbers is " + (x1+x2) );
                    }
                    break;
                    case 2:
                    {
                        x1 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1st integer."));
                        x2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 2nd integer"));
                        messagedialog("The area of the triangle is " + (x1*x2) );
                    }
                    break;
                    case 3:
                    {
                        x1 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter base."));
                        x2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter height"));
                        messagedialog("The are of the triangle is " + ((double)(0.5*x1*x2)));
                    }
                    case 4:
                    {
                        xx = Integer.parseInt(inputdialog("Choose numbers 1-9"));
                    {

                            if (xx==1)
                                messagedialog("You will be successful!");
                            else if (xx==2)
                                messagedialog("You will have a beautiful life. Blessings would come to you.");
                            else if (xx==3)
                                messagedialog("You are handsome/gorgeous. But.....");
                            else if (xx==4)
                                messagedialog("You must study well.");
                            else if (xx==5)
                                messagedialog("Unfortunately, you are unfortunate.");
                            else if (xx==6)
                                messagedialog("You will be an environmentalist. You will be called the Keeper of the Forest.");
                            else if (xx==7)
                                messagedialog("You are nothing but luck. You will be poured out with luck.");
                            else if (xx==8)
                                messagedialog("8. Nothing but a number. Try again.");
                            else
                                messagedialog("Endowed with the gift to serve others, people with the lucky number 9 are able to freely create an easy and relaxed atmosphere. /n Your humanity is welcomed among their friends. Besides, they are brilliant, funny, smart and generous. ");    
                        }
                    }
                    break;
                    default:
                        messagedialog("invalid");
                    }
            }
        else if (n2.equalsIgnoreCase("N"))
            System.exit(0);
        else
            messagedialog("INVALID");

    }

}

异常堆栈跟踪:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Choose numbers 1-9" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Exer29A.main(Exer29A.java:50) 

【问题讨论】:

  • NumberFormatException 出现在哪里?
  • #3 和#4 之间没有中断,不确定这是不是故意的。您对这种情况有何意见?
  • 您的错误实际上是java.lang.NumberFormatException。必须准确报告错误消息和异常以及所有程序输出。您应该在问题中包含整个堆栈跟踪,而不仅仅是与源代码无关的错误消息的一些错误版本。
  • 线程“主”java.lang.NumberFormatException 中的异常:对于输入字符串:java.lang.NumberFormatException.forInputString 中的“选择数字 1-9”(NumberFormatException.java:65)在 java.lang .Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at Exer29A.main(Exer29A.java:50)
  • 它会询问你的名字然后选择你想做什么然后如果你选择“4”来知道你的命运然后“选择数字1-9”然后该消息就会弹出。它不会在里面输出我的 if else 语句

标签: java swing exception-handling


【解决方案1】:

与所有其他调用不同,对于案例“4”,您调用 inputDialog(...) 而不是 JOptionPane.showInputDialog(null, "Enter base.")。这不是主要问题。

主要问题是什么 inputDialog(...) 返回。这和JOptionPane.showInputDialog(null, "Enter base.") 返回的不是一回事。

【讨论】:

  • inputdialog 调用JOptionPane.showInputDialog(null,s); 并像所有其他人一样返回String...
  • @MadProgrammer 确实如此,但它返回的是 which 字符串吗? (请不要回答这个问题)。
  • 我会尝试更改它等等。
  • 哇。案子解决了。多谢你们。我真的很难使用用户定义的方法。这是我们使用它的要求。无论如何。上帝保佑。 :)
  • @pcolor 基本上,随着您在编程中前进;一切都将在一个方法中。所以现在犯错是可以的,只要你从这些错误中吸取教训,你就可以在未来的几个月和几年里为自己省去很多痛苦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 2016-04-29
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多