Search code examples
javaexceptionswitch-statementjava-14

How can I replace throw exception with a System.out.print() in switch statement default case


Here I have a simple class to perform calculator operations.

Calculator.java

    private final ArrayList <Double> values;
    private final String operation;

    public Calculator(ArrayList<Double> values, String operation) {
        this.values = values;
        this.operation = operation;
    }

    public double calculate(){
        double num1 = values.get(0);
        double num2 = values.get(1);
        double answer;

        switch (operation) {
            case "+" :
                answer = addition(num1,num2);
                break;
            case "-" :
                answer = subtraction(num1,num2);
                break;
            case "*" :
                answer = multiplication(num1,num2);
                break;
            case "/" :
                answer = division(num1,num2);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + operation);
        }
        return answer;
    }

    public double addition(double num1,double num2){
        return num1+num2;
    }
    public double subtraction(double num1,double num2){
        return num1-num2;
    }
    public double multiplication(double num1,double num2){
        return num1*num2;
    }
    public double division(double num1,double num2){
        return num1/num2;
    }
}

Here I want to replace throw new IllegalStateException("Unexpected value: " + operation); this one with my own System.out the message and return nothing and stop program execution.

Here I can't use return 0 because this is a calculator.

Actually I want to know how to handle this exception properly with my own system out statement in cli without a red-letter exception message.

I have used JDK 14 here.


Solution

  • [...] and return noting and stop program execution.

    Since the method signature has a return type of double you have to return a result. You cannot return with no result.

    For that, you can change the method contract and make calculate method of return type void and access the calculated result via a separate method.

    private final ArrayList <Double> values;
    private final String operation;
    private Double result; //an instance variable to store the result.
    
    public void calculate() {
        Double num1 = values.get(0);
        Double num2 = values.get(1);
        switch (operation) {
            case "+" :
                result = addition(num1,num2);
                break;
            case "-" :
                result = subtraction(num1,num2);
                break;
            case "*" :
                result = multiplication(num1,num2);
                break;
            case "/" :
                result = division(num1,num2);
                break;
            default:
               //Your print statement here
        }
     }
    
    public Optional<Double> getResult() {
        return Optional.ofNullable(result);
    }
    

    A problem with this is what happens when a caller calls getResult() before calculate. I'll leave that to you.