samedi 8 mai 2021

Trying to reduce else if for errors into one

For the code below I am trying to reduce redundancies by putting the return errors into one else statement. Is there any way to do this? This is for an RPN calculator, with numbers being added to an array and if an operator is used, it will call a method to use that operator with the numbers in the listInput array.

      public String nextOperation() {
            for (String listInput : listInputs) {
                if (isLong(listInput)) {
                    if (current == null) {
                        current = new RPN(Long.parseLong(listInput));
                    } else {
                        addToStack(current, Long.parseLong(listInput));
                    }
                } else if (isBasicOperator(listInput)) {
                    if (current == null) {
                        return "Error: too few operands";
                    } else {
                        eval.evaluateBasic(current, listInput);
                    }
                } else {
                    if(isOperator(listInput)){
                        eval.evaluateRepeat(current,listInput);
                    }else{
                        return "Error: bad token " + "'" + listInput + "'";
                    }
                }
            }
            return getOutput();
        }

Aucun commentaire:

Enregistrer un commentaire