2011-11-08 3 views
0

私のプログラムでは、与えられた文字グレードと数値グレードからGPAを計算する必要があります。 最初のダイアログボックスでキャンセルをクリックするとnumberFormatException nullエラーが発生し、文字列を入力するとNumberFormatException入力文字列エラーが表示されます。 私は自分のプログラムでtry-catchを使用しなければならず、私はすでにtryしようとしています。とにかくtry-catchメソッドで例外を処理することができます。また、ユーザーはレターグレードと数値グレードのオプションを受け入れる 他には何もありません。Java、JOptionPaneの両方を処理する方法NumberFormatException入力文字列とNumberFormatException:Nullエラー?

次は、プログラムのコードです:

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

別のキャッチブロックで同じことをしますか?システムの終了は少し厳しいようですが。受け入れる?これは大きな方法です! –

答えて

0

あなたがやろうとしているものは100%確実ではないが、のtry/catchブロックで、あなたが好きなように多くの例外をキャッチし、異なっそれらを処理することができますso:

try { 
    do.something(); 
catch(NumberFormatException e){ 
    handleexception(e); 
}catch(IOException e){ 
    handleexception(e); 
}catch(Exception e){ 
    blow.up(); 
} 

などがあります。別のブロックで各例外タイプをキャッチすることができます。 NumberFormatExceptionをキャッチして、そこからどのような種類があるかを確認することもできます。これがうまくいきたいです

+0

ユーザーがJOptionPaneをキャンセルして文字列を入力したときにメッセージを表示したときにnullエラーを停止したい場合は、単一のtryとcatch(1つのキャッチブロック)を学習しただけで何ができますか?どちらも同じタイプの例外です。 – user1036587

+0

あなたは思考を変える必要があります。ヌルを扱うのにtry catchを使うのではなく、むしろそれを扱うべきです: 'if(userResponseNumerical!= null){ letsTry.dosomething(); } else {//はnullにする必要があります System.exit(0); } ' これを念頭に置いて問題に近づけてみてください。 (申し訳ありませんがコードを正しく表示することはできません) –

関連する問題