简体   繁体   中英

IllegalArgumentException Not functioning Correctly in Java

So throwing Illegal Exceptions is new to me and I was hoping for some pointers in it. I was making a code that will determine the amount of money generated or lost in an event, however, I was getting 2 errors in the code while testing. The first would be no matter what I input, I would receive the IllegalArgumentException error message "Exception in thread "main" java.lang.IllegalArgumentException : The letter chosen is invalid.Must be T, D, or E. The given data T will be ignored." I'm not quite sure why as even the message is saying that I was using the correct letter.

The second issue I was having was that my program was not actually showing values (aside from 0) in my display method. I felt like I had the correct idea so I thought maybe I did this incorrectly elsewhere. I did attach the entire code if needed, but I left a comment where I felt the error was.

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

Your parameter is a char, but you appear to be checking it against an int, and so the method addNewValue just doesn't make sense. You should compare chars with chars and ints with ints only.

A side recommendation: please understand that code formatting is very important and not optional or something that should be done carefully. If you want the best help here, please strive to make your code as easy to read and understand as possible. Also you will want to learn and follow Java naming conventions including using lower case letters to start variable names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM