在 JTextField 中使用 NumberFormatException
Using NumberFormatException in JTextField
所以我的程序要求用户输入两个整数,然后如果用户输入一个字符串,它将显示一条错误消息 "Invalid! Please input an integer"。但是在错误消息出现后它显示错误。
这是错误,但我设法将 JTextField 转换为字符串,然后再转换为整数
Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
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 SimpleCalculator1.main(SimpleCalculator1.java:52)
这是我的代码
import javax.swing.*;
public class SimpleCalculator1
{
public static void main(String [] args)
{
int error1, error2;
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JTextField field3 = new JTextField();
Object[] message =
{
"Enter first integer:", field1,
"Input second integer:", field2,
"Choose the operation to be used\nAddition(+)\nSubtraction(-)\nMultiplication(*)\nDivision(/):", field3,
};
int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
boolean shouldExit = false;
while(!shouldexit)
{
if(option == JOptionPane.OK_OPTION)
{
String value1 = field1.getText();
String value2 = field2.getText();
String operation = field3.getText();
try
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
switch(operation = field3.getText())
{
case "+":
case "Addition":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int result = num1+num2;
JOptionPane.showMessageDialog(null,"The sum is: "+result);
break;
}
case "-":
case "Subtraction":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int result = num1-num2;
JOptionPane.showMessageDialog(null,"The difference is: "+result);
break;
}
case "*":
case "Multiplication":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int result = num1*num2;
JOptionPane.showMessageDialog(null,"The product is: "+result);
break;
}
case "/":
case "Division":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
double result = (double)num1/num2;
JOptionPane.showMessageDialog(null,"The quotient is: "+result);
break;
}
default:
error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning.
}
option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
}
else if (option == JOptionPane.CANCEL_OPTION||)
{
JOptionPane.showMessageDialog(null,"Thank you for using our program!");
shouldExit = true;
}
}
}
即使您为字段 1 捕获 NumberFormatException
并显示错误,您在捕获后继续并尝试解析字段 3 中的数字,该数字不太可能被输入,然后您再次尝试将字段 1 和 2 解析为数值。扩展您的 catch 以包装所有 3 个字段的过程并强制用户重新输入有效值。
移动您的 catch 块以包括所有文本字段的解析并强制用户输入正确的值可以通过修改您的 while 循环来完成,如下所示:
boolean exit = false;
while(!exit) {
int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.OK_OPTION)
{
String value1 = field1.getText();
String value2 = field2.getText();
String operation = field3.getText();
try{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
operation = field3.getText();
if(operation != null){
operation = operation.toLowerCase();
}
switch(operation) {
case "+":
case "addition":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1+num2;
JOptionPane.showMessageDialog(null,"The sum is: "+result);
break;
case "-":
case "subtraction":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1-num2;
JOptionPane.showMessageDialog(null,"The difference is: "+result);
break;
case "*":
case "multiplication":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1*num2;
JOptionPane.showMessageDialog(null,"The product is: "+result);
break;
case "/":
case "division":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
double result = (double)num1/num2;
JOptionPane.showMessageDialog(null,"The quotient is: "+result);
break;
default:
error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning.
}
//option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
}
else if (option == JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(null,"Thank you for using our program!");
exit = true;
}
}
RCA : 首先检查在文本文件中输入的值是否是 try catch 中的整数。
try
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
无论它们是否是整数,然后您尝试将文本字段中的值转换为 switch 语句中的整数。然后 java 尝试将这些值转换为整数,但由于数字格式错误他不能。这样 java 就会显示该错误。
工作安排:将 switch 语句移到内部以尝试捕获检查文本字段值的位置。
创建整数 num1;整数 2;作为方法局部变量。
最终代码:
public static void main(String [] args)
{
int error1, error2;
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JTextField field3 = new JTextField();
int num1;
int num2;
Object[] message =
{
"Enter first integer:", field1,
"Input second integer:", field2,
"Choose the operation to be used\nAddition(+)\nSubtraction(-)\nMultiplication(*)\nDivision(/):", field3,
};
int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
boolean shouldexit = false;
while(!shouldexit)
{
if(option == JOptionPane.OK_OPTION )
{
String value1 = field1.getText();
String value2 = field2.getText();
String operation = field3.getText();
try
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
switch(operation = field3.getText())
{
case "+":
case "Addition":
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1+num2;
JOptionPane.showMessageDialog(null,"The sum is: "+result);
break;
}
case "-":
case "Subtraction":
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1-num2;
JOptionPane.showMessageDialog(null,"The difference is: "+result);
break;
}
case "*":
case "Multiplication":
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1*num2;
JOptionPane.showMessageDialog(null,"The product is: "+result);
break;
}
case "/":
case "Division":
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
double result = (double)num1/num2;
JOptionPane.showMessageDialog(null,"The quotient is: "+result);
break;
}
default:
error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning.
}
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
}
else
{
JOptionPane.showMessageDialog(null,"Thank you for using our program!");
shouldexit = true;
}
}}
所以我的程序要求用户输入两个整数,然后如果用户输入一个字符串,它将显示一条错误消息 "Invalid! Please input an integer"。但是在错误消息出现后它显示错误。
这是错误,但我设法将 JTextField 转换为字符串,然后再转换为整数
Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
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 SimpleCalculator1.main(SimpleCalculator1.java:52)
这是我的代码
import javax.swing.*;
public class SimpleCalculator1
{
public static void main(String [] args)
{
int error1, error2;
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JTextField field3 = new JTextField();
Object[] message =
{
"Enter first integer:", field1,
"Input second integer:", field2,
"Choose the operation to be used\nAddition(+)\nSubtraction(-)\nMultiplication(*)\nDivision(/):", field3,
};
int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
boolean shouldExit = false;
while(!shouldexit)
{
if(option == JOptionPane.OK_OPTION)
{
String value1 = field1.getText();
String value2 = field2.getText();
String operation = field3.getText();
try
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
switch(operation = field3.getText())
{
case "+":
case "Addition":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int result = num1+num2;
JOptionPane.showMessageDialog(null,"The sum is: "+result);
break;
}
case "-":
case "Subtraction":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int result = num1-num2;
JOptionPane.showMessageDialog(null,"The difference is: "+result);
break;
}
case "*":
case "Multiplication":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int result = num1*num2;
JOptionPane.showMessageDialog(null,"The product is: "+result);
break;
}
case "/":
case "Division":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
double result = (double)num1/num2;
JOptionPane.showMessageDialog(null,"The quotient is: "+result);
break;
}
default:
error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning.
}
option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
}
else if (option == JOptionPane.CANCEL_OPTION||)
{
JOptionPane.showMessageDialog(null,"Thank you for using our program!");
shouldExit = true;
}
}
}
即使您为字段 1 捕获 NumberFormatException
并显示错误,您在捕获后继续并尝试解析字段 3 中的数字,该数字不太可能被输入,然后您再次尝试将字段 1 和 2 解析为数值。扩展您的 catch 以包装所有 3 个字段的过程并强制用户重新输入有效值。
移动您的 catch 块以包括所有文本字段的解析并强制用户输入正确的值可以通过修改您的 while 循环来完成,如下所示:
boolean exit = false;
while(!exit) {
int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.OK_OPTION)
{
String value1 = field1.getText();
String value2 = field2.getText();
String operation = field3.getText();
try{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
operation = field3.getText();
if(operation != null){
operation = operation.toLowerCase();
}
switch(operation) {
case "+":
case "addition":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1+num2;
JOptionPane.showMessageDialog(null,"The sum is: "+result);
break;
case "-":
case "subtraction":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1-num2;
JOptionPane.showMessageDialog(null,"The difference is: "+result);
break;
case "*":
case "multiplication":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1*num2;
JOptionPane.showMessageDialog(null,"The product is: "+result);
break;
case "/":
case "division":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
double result = (double)num1/num2;
JOptionPane.showMessageDialog(null,"The quotient is: "+result);
break;
default:
error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning.
}
//option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
}
else if (option == JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(null,"Thank you for using our program!");
exit = true;
}
}
RCA : 首先检查在文本文件中输入的值是否是 try catch 中的整数。
try
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
无论它们是否是整数,然后您尝试将文本字段中的值转换为 switch 语句中的整数。然后 java 尝试将这些值转换为整数,但由于数字格式错误他不能。这样 java 就会显示该错误。
工作安排:将 switch 语句移到内部以尝试捕获检查文本字段值的位置。 创建整数 num1;整数 2;作为方法局部变量。
最终代码:
public static void main(String [] args)
{
int error1, error2;
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JTextField field3 = new JTextField();
int num1;
int num2;
Object[] message =
{
"Enter first integer:", field1,
"Input second integer:", field2,
"Choose the operation to be used\nAddition(+)\nSubtraction(-)\nMultiplication(*)\nDivision(/):", field3,
};
int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
boolean shouldexit = false;
while(!shouldexit)
{
if(option == JOptionPane.OK_OPTION )
{
String value1 = field1.getText();
String value2 = field2.getText();
String operation = field3.getText();
try
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
switch(operation = field3.getText())
{
case "+":
case "Addition":
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1+num2;
JOptionPane.showMessageDialog(null,"The sum is: "+result);
break;
}
case "-":
case "Subtraction":
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1-num2;
JOptionPane.showMessageDialog(null,"The difference is: "+result);
break;
}
case "*":
case "Multiplication":
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1*num2;
JOptionPane.showMessageDialog(null,"The product is: "+result);
break;
}
case "/":
case "Division":
{
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
double result = (double)num1/num2;
JOptionPane.showMessageDialog(null,"The quotient is: "+result);
break;
}
default:
error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning.
}
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
}
else
{
JOptionPane.showMessageDialog(null,"Thank you for using our program!");
shouldexit = true;
}
}}