0
    Scanner scan = new Scanner(System.in);

    System.out.print("Enter 2 numbers: ");
    int num1 = scan.nextInt();
    int num2 = scan.nextInt();
    System.out.println("Numbers Saved! Choose your operator + - * / ");
    String operator = scan.next();
    int result;

    result = (operator.equals("+")) ? (num1 + num2) : (num1);
    System.out.println("The addition result is " +result);
    result = (operator.equals("-")) ? (num1 - num2) : (num1);
    System.out.println("The subtraction result is " +result);
    result = (operator.equals("*")) ? (num1 * num2) : (num1);
    System.out.println("The multiplication result is " +result);
    result = (operator.equals("/")) ? (num1 / num2) : (num1);
    System.out.println("The division result is " +result);



}

} This is my simple calculator code, for example when i choose the + option, it runs all System.out.println lines, how do I prevent it from doing this and only execute the println that matches the operator?

5
  • 1
    Don't shove everything into a ternary... why did you do this? Commented Mar 17, 2019 at 19:14
  • 1
    Try using else-if statements. Commented Mar 17, 2019 at 19:16
  • Replace your ternar operators with if-else statements. And print only when the right condition is met. Commented Mar 17, 2019 at 19:18
  • @Ugness I did it with else if statements and switch, I just wanted to try it in ternary Commented Mar 17, 2019 at 19:26
  • @Hotam, Ternary operation are only used to return a conditional value, here you are trying to execute a block of code. The adequate way is by branching with if-else, or switch as suggested by Ugnes and MWB Commented Mar 17, 2019 at 19:31

2 Answers 2

2

Try a switch:

switch (operator) {
    case "+":
        result = num1 + num2;
        System.out.println("The addition result is " + result);
        break;
    case "-":
        result = num1 - num2;
        System.out.println("The subtraction result is " + result);
        break;
    case "-":
        result = num1 * num2;
        System.out.println("The multiplication result is " + result);
        break;
    case "/":
        result = num1 / num2;
        System.out.println("The integer division result is " + result);
        break;
    default:
        throw IllegalArgumentException("Unsupported operator: " + operator);
}
Sign up to request clarification or add additional context in comments.

Comments

2

I would encapsulate the operator logic (resolution and evaluation) into an enum. Like,

enum Oper {
    ADDITION("+"), SUBTRACTION("-"), MULTIPLICATION("*"), DIVISION("/");
    Oper(String symbol) {
        this.symbol = symbol;
    }

    public int eval(int a, int b) {
        switch (this) {
        case ADDITION: return a + b;
        case SUBTRACTION: return a - b;
        case MULTIPLICATION: return a * b;
        case DIVISION: return a / b;
        }
        return -1;
    }

    private String symbol;

    public static Oper from(String operator) {
        for (Oper o : values()) {
            if (o.symbol.equals(operator)) {
                return o;
            }
        }
        return null;
    }
}

That simplifies the logic in main, just resolve the operator and evaluate it. Like,

Oper o = Oper.from(operator);
System.out.printf("The %s result is %d%n", o.name().toLowerCase(), o.eval(num1, num2));

1 Comment

There is rarely a need to switch over enum values inside the enum. Define an abstract method, and put the eval logic in per-value methods. (Or pass a functional parameter like BinaryIntOperator to the ctor).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.