2 Replies - 3602 Views - Last Post: 03 February 2011 - 11:13 PM Rate Topic: -----

#1 ellemrac721  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-February 11

Need help, postfix calculator with stack

Posted 03 February 2011 - 06:33 PM

Hi! I really need some help with a previous assignment we had because it's driving me crazy. This was my first version of the postfix calculator, but I had to abandon the idea and make a new one because I couldn't get this one to work in time for our submission deadline :P I've tried everything I can think of but I still keep getting the wrong answer when I try out the code I wrote below. Things start to go wrong by the "else if(isOperator(s) == true)" part. I don't understand if I wrote anything wrong, but for some reason, it just returns 0 all the time.

public String calculateFail(String input)
    {
        StringTokenizer st = new StringTokenizer(input);
        Integer temp = 0;
        for(int n = 0; n < (st.countTokens() + 1); n++){
            String s = st.nextToken();            
            if(isInteger(s) == true){
                calc.push(Integer.parseInt(s));
            }
            else if(isOperator(s) == true){
                temp = calc.pop();
                Integer i = calc.pop();
                if(s.equals("+")){
                    temp = i + temp;
                } else if(s.equals("-")){
                    temp = i - temp;
                } else if(s.equals("*")){
                    temp = i * temp;
                } else if(s.equals("/")){
                    temp = i / temp;
                }       
                
                if(calc.size() == 0){
                    calc.push(i);
                }
            }
            
            else{
                System.out.println("Please enter a valid postfix expression.");
            }
        }
        return temp.toString();
    }



And here's the code for the isInteger and isOperator methods that the method above uses:
    private boolean isInteger(String in){
        try{
            int x = Integer.parseInt(in);
            return true;
        }

        catch(NumberFormatException error){
            return false;
        }
    }

    /**
     * Private method that checks if the String is an operator.
     * Used in the calculatorFail method.
     * 
     * @return  Returns true if the String is an operator.
     */
    private boolean isOperator(String in){
        String[] op = {"+", "-", "*", "/"};
        for (int i = 0; i < op.length; i++){
            if(in == op[i]){
                return true;
            }
        }
        return false;
    }



Many thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Need help, postfix calculator with stack

#2 Alex_H  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 27-January 11

Re: Need help, postfix calculator with stack

Posted 03 February 2011 - 07:53 PM

The problem you are encountering is with the line
in == op[i]
in the isOperator() method.

Whenever you are comparing two strings, you should always use the equals operator that is in the string class. So line 22 of the second block of code above should be written as:


  if(in.equals(op[i])){ 


Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 ellemrac721  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-February 11

Re: Need help, postfix calculator with stack

Posted 03 February 2011 - 11:13 PM

Hi! Thanks for the reply! I tried your suggestion but it still didn't work :( But of course it should have been like that from the beginning, I changed it now :)

private boolean isOperator(String in){
        String[] op = {"+", "-", "*", "/"};
        for (int i = 0; i < op.length; i++){
            if(in.equals(op[i])){;
                return true;
            }
        }
        return false;
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1