我的错误是java.lang.NumberFormatException
我正在使用jcreator。它表示该过程已完成,但当您转到切换,然后选择案例4,然后选择数字1-9时,它表示java.numberformatexception。这是什么原因造成的?我如何修复它?我只是个初学者我的错误是java.lang.NumberFormatException,java,swing,exception-handling,Java,Swing,Exception Handling,我正在使用jcreator。它表示该过程已完成,但当您转到切换,然后选择案例4,然后选择数字1-9时,它表示java.numberformatexception。这是什么原因造成的?我如何修复它?我只是个初学者 import javax.swing.JOptionPane; public class Exer29A { public static String inputdialog(String s) { JOptionPane.showInputDialo
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)
与所有其他调用不同,对于案例“4”,您调用inputDialog。。。输入base,而不是JOptionPane.showInputDialognull。。这不是主要问题
主要问题是什么输入对话框。。。返回。这与JOptionPane.showInputDialognull中输入base的内容不同。返回。数字格式异常发生在哪里?3和4之间没有中断,不确定这是否是故意的。您对此条件的输入是什么?您的错误实际上是java.lang.NumberFormatException。必须准确报告错误消息、异常以及所有程序输出。您应该在问题中包含整个堆栈跟踪,不仅仅是一些与源代码不相关的错误消息的损坏版本。线程主java.lang.NumberFormatException中的异常:对于输入字符串:在java.lang.NumberFormatException.forInputStringNumberFormatException.java:65在java.lang.Integer.parseIntInteger.java:580在java:615在Exer29A.mainPer29a.java:50它会问你的名字,然后选择你想做什么,如果你选择4知道你的命运,然后选择数字1-9,然后消息就会弹出。它不会在InputDialog调用JOptionPane.showInputDialognull,s中输出我的if-else语句;并像所有其他字符串一样返回一个字符串…@MadProgrammer这是真的,但它返回哪个字符串?请不要回答这个问题。我会试着改变的。等等。哇。案件得到解决。谢谢各位。Im在用户定义的方法方面确实有困难。我们必须使用它。无论如何。上帝保佑@pcolor基本上是在编程过程中进行的;一切都会在一个方法中。所以现在犯错误是可以的,只要你从这些错误中吸取教训,你就可以在未来的几个月和几年里为自己省下很多痛苦。