1

I have a working piece of code for calculating polish notation, would there be any way to change the code i have currently to perform reverse polish notation or will it need to be a different solution entirely. Thanks.

import java.util.Scanner;

public class polishNotation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(calculator(scanner));
}

public static int calculator(Scanner scanner) {
    if (scanner.hasNextInt()) {
        return scanner.nextInt();
    }
    char operator = scanner.next().charAt(0);
    int number1 = calculator(scanner);
    int number2 = calculator(scanner);

    if (operator=='+') {
        return number1 + number2;
    } 
    else if(operator=='-') {
        return number1 - number2;
    } 
    else if (operator=='/') {
        return number1 / number2;
    } 
    else {
        return number1 * number2;
    } 

}

}

Gaben
  • 21
  • 3
  • Since it appears you are simply evaluating two operands and an operator, all you need to do is prompt for the operator **after** entering the operands rather than **before**. But this doesn't seem like a real RPN exercise to me. – WJS Mar 04 '20 at 15:37

0 Answers0