IllegalArgumentException在Java中无法正常运行

时间:2016-04-24 04:07:46

标签: java illegalargumentexception

所以抛出非法例外对我来说是新的,我希望能有一些指示。我正在制作一个代码来确定事件中产生或丢失的金额,但是,我在测试时遇到了2个错误。第一个是无论我输入什么,我都会收到IllegalArgumentException错误消息“线程中的异常”主“java.lang.IllegalArgumentException:所选的字母无效。必须是T,D或E.给定数据T将被忽略。“我不太清楚为什么甚至连消息都说我使用了正确的字母。

我遇到的第二个问题是我的程序实际上并没有在我的显示方法中显示值(除了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;    
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的参数是一个char,但您似乎是在针对int进行检查,因此方法{{1}}只是没有意义。你应该将字符与字符和整数仅与整数进行比较。

一个侧面建议:请理解代码格式是非常重要而不是可选的或应该仔细完成的事情。如果您需要最好的帮助,请尽量使您的代码易于阅读和理解。您还需要学习并遵循Java命名约定,包括使用小写字母来启动变量名称。