Java,如何处理JOptionPane NumberFormatException输入字符串和NumberFormatException:Null错误?
在我的程序中,我必须根据给定的字母等级和数字等级计算平均成绩。 但是,在第一个对话框中,如果单击“取消”,则会出现numberFormatException null错误;如果键入字符串,则会出现numberFormatException输入字符串错误。 我必须在我的程序中使用try-catch,我已经尝试过了。我是否可以用try-catch方法处理这两个异常。我还希望用户可以接受地为字母等级和数字等级选项键入1和2,并且 没有别的了 以下是该程序的代码:Java,如何处理JOptionPane NumberFormatException输入字符串和NumberFormatException:Null错误?,java,Java,在我的程序中,我必须根据给定的字母等级和数字等级计算平均成绩。 但是,在第一个对话框中,如果单击“取消”,则会出现numberFormatException null错误;如果键入字符串,则会出现numberFormatException输入字符串错误。 我必须在我的程序中使用try-catch,我已经尝试过了。我是否可以用try-catch方法处理这两个异常。我还希望用户可以接受地为字母等级和数字等级选项键入1和2,并且 没有别的了 以下是该程序的代码: import javax.swing.
import javax.swing.JOptionPane;
class CalcGPA{
public static void main (String [] args){
String questionOfOptions = "What type of grade do you wish to enter?\n";
String letterGradeOption = "Press 1- for letter (A,C,etc)\n";
String numericGradeOption= "Press 2-for numeric(89,68,etc)";
String message= questionOfOptions
+letterGradeOption
+numericGradeOption;
String endingMessage = "The corresponding GPA is";
String calculatedGPA="0" ;
String randomString="kc;lc[ ams[xas";
String userResponseString="0";
int userResponseNumerical=0;
char characterValue='0';
do {
userResponseString=JOptionPane.showInputDialog
(null,
message,
"GPA Calculator",
JOptionPane.OK_CANCEL_OPTION);
try {
userResponseNumerical= Integer.parseInt(userResponseString);
} catch(NumberFormatException.forInputString ex) {
System.exit(0);
}
userContinuationOption= JOptionPane.showConfirmDialog
(null,
"Do You Want to Continue? ",
"GPA Calculator",
JOptionPane.YES_NO_OPTION);
if(userContinuationOption==JOptionPane.NO_OPTION) {
System.exit(JOptionPane.NO_OPTION);
}
switch(userResponseNumerical){
case 1:
String userGradeResponse = JOptionPane.showInputDialog
(null,
"Enter a Grade.",
"GPA Calculator",
JOptionPane.INFORMATION_MESSAGE);
if (userGradeResponse.equals("A")) {
calculatedGPA ="4.";
} else if (userGradeResponse.equals("B")) {
calculatedGPA=" 3.";
} else if (userGradeResponse.equals("C")) {
calculatedGPA=" 2.";
} else if (userGradeResponse.equals("D")) {
calculatedGPA=" 1.";
} else if(userGradeResponse.equals("F")) {
calculatedGPA=" 0.";
} else {
JOptionPane.showMessageDialog
(null,
"Improper input ",
"Error",
JOptionPane.ERROR_MESSAGE);
break;
}
JOptionPane.showMessageDialog
(null,
endingMessage+" "+calculatedGPA,
"GPA Calculator",
JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
String userNumericString= JOptionPane.showInputDialog
(null,
"Enter a Numeric Grade.",
"GPA Calculator",
JOptionPane.INFORMATION_MESSAGE);
int userNumericGradeResponse=0;
Integer.parseInt(userNumericString);
if(userNumericGradeResponse>=80) {
calculatedGPA=" 4.";
} else if (userNumericGradeResponse>70) {
calculatedGPA=" 3.";
} else if (userNumericGradeResponse>60) {
calculatedGPA=" 2.";
} else if (userNumericGradeResponse>50) {
calculatedGPA=" 1.";
} else if(userNumericGradeResponse>=0 &&
userNumericGradeResponse<50) {
calculatedGPA=" 0.";
}
break;
default:
JOptionPane.showMessageDialog
(null,
"Improper input",
"Error",
JOptionPane.INFORMATION_MESSAGE);
break;
}
int userContinuationOption = JOptionPane.showConfirmDialog
(null,
"Do You Want to Continue? ",
"GPA Calculator",
JOptionPane.YES_NO_OPTION);
if(userContinuationOption==JOptionPane.NO_OPTION) {
System.exit(JOptionPane.NO_OPTION);}
}
while(true);
}
}
import javax.swing.JOptionPane;
类CalcGPA{
公共静态void main(字符串[]args){
String questionOfOptions=“您希望输入什么类型的分数?\n”;
String letterGradeOption=“按1-字母(A、C等)\n”;
String numericGradeOption=“按2-for numeric(89、68等)”;
字符串消息=问题选项
+信用等级选项
+数值期权;
String endingMessage=“相应的GPA为”;
字符串calculatedGPA=“0”;
String randomString=“kc;lc[ams[xas”;
字符串userResponseString=“0”;
int userresponseEnumerical=0;
characterValue='0';
做{
userResponseString=JOptionPane.showInputDialog
(空,
消息
“GPA计算器”,
JOptionPane.OK\u CANCEL\u选项);
试一试{
UserResponseEnumerical=Integer.parseInt(userResponseString);
}catch(NumberFormatException.forInputString ex){
系统出口(0);
}
userContinuationOption=JOptionPane.showConfirmDialog
(空,
“是否要继续?”,
“GPA计算器”,
JOptionPane.YES\u NO\u选项);
if(userContinuationOption==JOptionPane.NO_选项){
退出系统(JOptionPane.NO_选项);
}
开关(UserResponseEnumerical){
案例1:
字符串userGradeResponse=JOptionPane.showInputDialog
(空,
“输入分数。”,
“GPA计算器”,
JOptionPane.INFORMATION(信息和消息);
if(userGradeResponse.equals(“A”)){
calculatedGPA=“4。”;
}else if(userGradeResponse.equals(“B”)){
calculatedGPA=“3。”;
}else if(userGradeResponse.equals(“C”)){
calculatedGPA=“2。”;
}如果(userGradeResponse.equals(“D”){
calculatedGPA=“1。”;
}else if(userGradeResponse.equals(“F”)){
calculatedGPA=“0。”;
}否则{
JOptionPane.showMessageDialog
(空,
“输入不当”,
“错误”,
JOptionPane.ERROR\u消息);
打破
}
JOptionPane.showMessageDialog
(空,
endingMessage+“”+calculatedGPA,
“GPA计算器”,
JOptionPane.INFORMATION(信息和消息);
打破
案例2:
字符串userNumericString=JOptionPane.showInputDialog
(空,
“输入数字等级。”,
“GPA计算器”,
JOptionPane.INFORMATION(信息和消息);
int userNumericGradeResponse=0;
整数.parseInt(userNumericString);
如果(UserNumericGrade响应>=80){
calculatedGPA=“4。”;
}否则如果(UserNumericGrade响应>70){
calculatedGPA=“3。”;
}否则如果(UserNumericGrade响应>60){
calculatedGPA=“2。”;
}否则如果(userNumericGradeResponse>50){
calculatedGPA=“1。”;
}如果(userNumericGradeResponse>=0),则为else&&
userNumericGradeResponse无法100%确定您正在尝试执行的操作,但使用try/catch块,您可以捕获任意数量的异常,并以不同方式处理它们,因此:
try {
do.something();
catch(NumberFormatException e){
handleexception(e);
}catch(IOException e){
handleexception(e);
}catch(Exception e){
blow.up();
}
诸如此类。你应该能够在不同的块中捕获每个异常类型。但是你也可以捕获NumberFormatException
,然后从那里检查什么类型的异常。希望这有助于在另一个捕获块中做同样的事情?尽管系统退出看起来有点苛刻。接受性?这是一个非常大的方法!我哈我只学会了一次尝试和捕捉(只有一个捕捉块),如果我想在用户取消JOptionPane时停止null错误,并在输入字符串时给出消息,我能做什么?它们都是相同类型的异常。您需要改变想法,您不应该使用try-catch来处理null,而应该处理它:if(userResponseNumerical!=null){letsTry.dosomething();}否则{//必须为空系统。退出(0);}
请尝试解决此问题。(很抱歉,无法正确显示代码)