【问题标题】:Java, How to handle both JOptionPane NumberFormatException input string and NumberFormatException:Null error?Java,如何处理 JOptionPane NumberFormatException 输入字符串和 NumberFormatException:Null 错误?
【发布时间】:2011-11-08 22:39:01
【问题描述】:

在我的程序中,我必须根据给定的字母等级和数字等级来计算 GPA。 但是,在第一个对话框中,如果我单击取消,它会给我 numberFormatException null 错误,如果我键入 string,它会给我 NumberFormatException 输入字符串错误。 我必须在我的程序中使用 try-catch 并且我已经尝试过了。无论如何我可以使用 try-catch 方法处理这两个异常。我还希望用户能够接受地为字母等级和数字等级选项键入 1 和 2 并且 没有别的。

以下是程序的代码:

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);  
        }
    }

【问题讨论】:

  • 在另一个 catch 块中做同样的事情?虽然系统退出似乎有点苛刻。接受吗?这是一个非常强大的方法!

标签: java


【解决方案1】:

不是 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(); }else{//must be null System.exit(0); } 试着带着这个想法来解决这个问题。 (抱歉无法正常显示代码)
猜你喜欢
  • 1970-01-01
  • 2021-03-20
  • 2017-08-24
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 1970-01-01
  • 2011-05-23
相关资源
最近更新 更多