Search code examples
javastackcalculatornew-operator

stuck on rpn calculator problem java java.lang.String[]


ive been trying to make a reverse polish notation calculator in java but i keep getting errors i dont understand the cause of.

       private int tokens = ["2","1","+","3","*"]
public class Solution {
    public int evalRPN(String[] tokens) {
        int a,b;
        Stack<Integer> S = new Stack<Integer>();
        for (String s : tokens) {
            if(s.equals("+")) {
                S.add(S.pop()+S.pop());
            }
            else if(s.equals("/")) {
                b = S.pop();
                a = S.pop();
                S.add(a / b);
            }
            else if(s.equals("*")) {
                S.add(S.pop() * S.pop());
            }
            else if(s.equals("-")) {
                b = S.pop();
                a = S.pop();
                S.add(a - b);
            }
            else {
                S.add(Integer.parseInt(s));
            }
        }   
        return S.pop();
    }
}

ive just made an instance of this in the test class and tried running it intelijij is saying given: string, required string[] or

java: method evalRPN in class calculator cannot be applied to given types;
  required: java.lang.String[]
  found:    no arguments
  reason: actual and formal argument lists differ in length

Solution

  • I can see that you're coming from python. Here are some things to fix up about your code.

    1) You must end every variable with ; in java.

    2) It seems like you want to make an array of Strings in java. As seen here

    private int tokens = ["2","1","+","3","*"];
    

    the problem is when you say a tokens is an int you have to make it one. So what you really want is a String[] aka a String array. So first change tokens to this below.

    private String[] tokens = ["2","1","+","3","*"];
    

    But wait this will still give you an error because in java arrays start and end with {} So change it to this below and tokens will work

    private String[] tokens = {"2","1","+","3","*"}; // <-- make sure to add ;
    

    3) public int evalRPN(String[] tokens) is a method that needs a String[] array passed into it. You have to actually call that method for it to run just making private String[] tokens = {"2","1","+","3","*"}; isn't going to do anything.

    Anyways heres a working example below you can copy and paste the code and run it here (https://www.programiz.com/java-programming/online-compiler/). But right after you do that I highly recommend you watch this (https://youtu.be/eIrMbAQSU34) you need to clear up a lot of your miss conceptions about java. I can't explain them all in this post.

    import java.util.*; // Import Stack
    class HelloWorld {
        
        
        public static void main(String[] args) {
            String[] tokens = {"2","1","+","3","*"}; // <-- make sure to add ;
            
            int value = evalRPN(tokens); // This is what I mean about calling a method
            System.out.println(value);
         
        }
      
        public static int evalRPN(String[] tokens) {
            int a,b;
            Stack<Integer> S = new Stack<Integer>();
            for (String s : tokens) {
                if(s.equals("+")) {
                    S.add(S.pop()+S.pop());
                }
                else if(s.equals("/")) {
                    b = S.pop();
                    a = S.pop();
                    S.add(a / b);
                }
                else if(s.equals("*")) {
                    S.add(S.pop() * S.pop());
                }
                else if(s.equals("-")) {
                    b = S.pop();
                    a = S.pop();
                    S.add(a - b);
                }
                else {
                    S.add(Integer.parseInt(s));
                }
            }   
            return S.pop();
        }
    
    }