Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Try and Catch Confusion

  1. Nov 16, 2006 #1
    I am evaluating a postfix expression using stacks and stuff. Below is the method I made. But I am not sure where to put the try/catch satement and what is it that i have to catch.

    Code (Text):
        public int evaluateRPN()
        {
            StringTokenizer token = StringTokenizer(postfix);
            stk.clear();
           
            try
            {
                while(token.hasMoreTokens())
                {
                    String s = token.nextToken();
                   
                    if(isNumber(s))
                        stk.push(new Integer(Integer.parseInt(s)));
                    else
                    {
                        char operator = s.charAt(0);
                        int x1 = ((Integer)stk.pop()).valueOf();
                        int x2 = ((Integer)stk.pop()).valueOf();
                       
                        int x3;
                       
                        switch(operator)
                        {
                            case StackConstants.ADDITION_OPERATOR:
                                x3 = x2 + x1;
                                stk.push(new Integer(x3));
                            break;
                           
                            case StackConstants.SUBTRACTION_OPERATOR:
                                x3 = x2 - x1;
                                stk.push(new Integer(x3));
                            break;
                           
                            case StackConstants.MULTIPLICATION_OPERATOR:
                                x3 = x2 * x1;
                                stk.push(new Integer(x3));
                            break;
                           
                            case StackConstants.DIVISION_OPERATOR:
                                x3 = x2 / x1;
                                stk.push(new Integer(x3));
                            break;
                                               
                            case StackConstants.MODULUS_OPERATOR:
                                x3 = x2 % x1;
                                stk.push(new Integer(x3));
                            break;
                        }
                    }
                }
            }
            catch(EmptyStackException e)
            {
               
            }
           
            return ((Integer)stk.pop()).valueOf();
        }
     
  2. jcsd
  3. Nov 16, 2006 #2

    0rthodontist

    User Avatar
    Science Advisor

    You should put the try/catch around the Integer.parseInt() call. It will throw a NumberFormatException if the string you are trying to parse into an integer is not an integer, like the string "abc."
     
  4. Nov 16, 2006 #3

    verty

    User Avatar
    Homework Helper

    I don't think so, that parseInt() is only when isNumber(). I think the try/catch should catch malformed sequences like '2+' (and I'm not saying where that is because I imagine this is homework).
     
  5. Nov 16, 2006 #4

    verty

    User Avatar
    Homework Helper

    Oh, silly me, there is already a try/catch around almost the entire thing. The only place left is the return statement (which will error on empty stack). Stupid question. Although, if you want to catch malformed sequences you can still do that.
     
    Last edited: Nov 16, 2006
  6. Nov 16, 2006 #5

    verty

    User Avatar
    Homework Helper

    Having thought about this more, I realise that malformed sequences will always result in an empty stack because the stack only ever contains numbers. I had in mind a stack containing the complete expression, whereas this one only contains intermediate results.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Try and Catch Confusion
  1. Pointer confusion (Replies: 1)

  2. Makefile confusion (Replies: 34)

Loading...