2016-04-24 20 views
0

不正な例外を投げることは私には新しく、その中にいくつかのポインタが必要です。私はイベントで発生または損失の金額を決定するコードを作っていましたが、テスト中にコードに2つのエラーが発生しました。最初に入力したものに関係なく、IllegalArgumentExceptionエラーメッセージ "例外スレッド"メイン "java.lang.IllegalArgumentException:選択された文字は無効です.T、D、またはEでなければなりません。指定されたデータTは無視されます。私はメッセージが正しい文字を使用していると言っているのはなぜか分かりません。IllegalArgumentException正しく機能していませんJavaで正しく

2番目の問題は、自分のプログラムで実際に表示方法に値(0以外の値)が表示されていないことでした。私は正しいアイデアがあるように感じたので、別の場所でこれを間違ってやったかもしれないと思った。必要に応じてコード全体を添付しましたが、エラーが発生したと思われるコメントを残しました。

public class EdmondEventManager2 { 

     /**Purpose of code is to determine the amount of money generated or lost in an event 
     * @param args the command line arguments 
     */ 
     public static void main(String[] args) { 
      EdmondEventClass object= new EdmondEventClass(); 

      object.addNewValue(amountType(),validateAmount()); 
      object.displayResults(); 
     } 

     /*static method that will read and validate amount type T-ticket sales, D-Donation, E-expenses */ 
     public static char amountType(){ 
      Scanner keyboard= new Scanner (System.in); 
      System.out.println("Please select the amount Type: T-Ticket Sales," 
         + " D-Donations, E-Expenses"); 
      char amtType=keyboard.next().charAt(0); 
      amtType=Character.toUpperCase(amtType); 

      switch(amtType) 
      { 
       case 'T' &'t': 
        break; 
       case 'D'& 'd' : 
        break; 
       case 'E' &'e' : 
        break; 
      } 

      if (amtType !='T'&& amtType!='D'&& amtType!='E'&&amtType!='t' 
        &&amtType!='d'&&amtType!='e'){ 
       do {  
        System.out.println("Choice invalid. Please select T, D, or E"); 
        System.out.println("Enter amount type: T, D, or E."); 
        amtType=keyboard.next().charAt(0); 
       }while (amtType!='T'&&amtType!='D'&&amtType!='E'); 
       return amtType= Character.toUpperCase(amtType); 
      } 

      return amtType= Character.toUpperCase(amtType); 
     } 

     /*static method that will read and validate the amount. I believe te first issue is here */ 
     public static double validateAmount(){ 
     Scanner keyboard= new Scanner (System.in); 
     System.out.println("Please enter the amount in dollars"); 
     double amount=keyboard.nextInt(); 

     if (amount<=0){ 
      do {  
       System.out.println("Amount must be a positive number!" 
        + " Try Again"); 
       System.out.println("Please enter the amount in dollars"); 
       amount=keyboard.nextInt(); 
      }while (amount<=0); 
       return amount; 
     } 

     return amount;     
     } 

     public static class EdmondEventClass{ 
     private int T; 
     private int D; 
     private int E; 

     //constructors 
     public EdmondEventClass(){  
      T=0; 
      D=0; 
      E=0;  
     } 

     //getters 
     public int getTotalIncomeSale() { 
      return this.T; 
     } 

     public int getTotalDonatins() { 
      return this.D; 
     } 

     public int getTotalExpenses(){ 
      return this.E; 
     } 

     /*Instance method that will add a new value to one of the totals 
     *@char amtType- One of the letters the user must chose. T-ticket sales, 
      D-Donation, E-expenses 
     *@ double amount- the amount for the chosen amtType.  
     */ 
     public double addNewValue (char amtType, double amount) { 

      if (amount<=0) { 
       throw new IllegalArgumentException("Amount must be positive and " 
       + "non-zero. The given data " +amount+ " will be ignored."); 
      } 
      else if (amtType !=T&& amtType!=D&& amtType!=E) { 
       throw new IllegalArgumentException ("The letter chosen is invalid." 
        + "Must be T, D, or E. The given data " +amtType+ 
        " will be ignored"); 
      } 

      return amount + amtType; 
     } 

     //Will display the outcome of the event. I believe the second issue is here. 
     public double displayResults(){ 
      System.out.println ("Event Overall Outcome:"); 
      System.out.println ("Total ticket sales:  "+T); 
      System.out.println ("Total donations:  "+D+ " +"); 
      System.out.println ("      -------"); 
      System.out.println ("Total income: "); 
      System.out.println ("Total expense:   "+E+ " -"); 
      System.out.println ("      -------"); 
      System.out.println ("Event profits: "); 
      String numberString=String.format ("%8.2f"); 
      return T+D;  
     } 
    } 
} 
+0

実際のエラーを表示できますか? – pczeus

+0

以下の@pczeusは、IllegalArgumentExceptionを使用しようとしたときに受け取るエラーです。申し訳ありませんが、私はコメントを破る必要があったコメントは、それを奇妙にフォーマットし続けた –

+0

スレッド "main"の例外java.lang.IllegalArgumentException:選択された文字は無効ですT、D、またはEでなければなりません。 edmondeventmanager2.EdmondEventManager2.mainでedmondeventmanager2.EdmondEventManager2 $ EdmondEventClass.addNewValue(EdmondEventManager2.java:121) \t(EdmondEventManager2.java:24) Cで \tは:\ユーザーは、私のPC当たり前\のAppData \ローカル\ NetBeansの\キャッシュを\ \ 8.1 \ executor-snippets \ run.xml:53:Javaが返されました:1 BUILD FAILED(合計時間:4秒) –

答えて

2

あなたのパラメータは文字ですが、int型に対してそれをチェックしているように見える、とこれだけ意味がありませんaddNewValue方法。 charsとintをintと比較する必要があります。

サイド推奨:コードフォーマットは非常にであり、オプションではなく、注意深く行う必要があることを理解してください。ここで最善の助けが必要な場合は、できるだけ読みやすく理解しやすいようにコードを作成してください。また、変数名を始めるために小文字を使用することを含むJava命名規則を学び、従うことが望ましいでしょう。

+0

ありがとうございます。私は最初にパラメータをintにしようとしましたが、私はまだ間違った考え方をしていたかもしれないと思っていたので、エラーメッセージを受け取りました。私は最終的にそれを理解するだろうと確信しています。コードの書式設定のためのポインタをありがとう。私はそれについてあまり知らないが、再び私はそれを数週間笑にしてきた。 –

関連する問題