-2

I'm trying to make an RPN calculator. I have done the conversion from infix to postfix, now I want to evaluate the converted expression. When I enter any expression I get error

String index out of range: 1.

Here's my code with what I'm supposed to do in the program:

static int eval(String postfix) {
    int result = 0;
    String temp2 = "";
    int num1, num2, OPS;
    char operator;
    String delete = "";


    for (int i = 0; i < postfix.length(); i++) {
        char M = postfix.charAt(i);

        // if v_i is an operand: Push v_i to tmp2.
        if (Character.isDigit(postfix.charAt(i))) {
            temp2 = M + temp2;
        }

        /*
         * if v_i is an operator: Apply v_i to the top two elements of tmp2.
         * Replace these by the result in tmp2.
         */
        if (postfix.charAt(i) == '+' || postfix.charAt(i) == '-' || postfix.charAt(i) == '*'
                || postfix.charAt(i) == '/') {

            temp2 = M + temp2.substring(2);

        }

        while (postfix.charAt(0) != '0') {
            num1 = Character.getNumericValue(temp2.charAt(temp2.length()-1));
            delete = delete.substring(0,i);
            operator = postfix.charAt(i);

            num2 = Character.getNumericValue(temp2.charAt(temp2.length()+i));
                    //Integer.parseInt(postfix.substring(0,i));

            result = num1 + num2;
            result = num1 - num2;
            result = num1 * num2;
            result = num1 / num2;

            switch (operator) {

            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;

            }
        }


        if (temp2.length() != 0) {
            temp2 = result + temp2;
        }

    }
    return result;
}

I get the error in this part:

while (postfix.charAt(0) != '0') {
            num1 = Character.getNumericValue(temp2.charAt(temp2.length()-1));
            delete = delete.substring(0,i);
            operator = postfix.charAt(i);

            num2 = Character.getNumericValue(temp2.charAt(temp2.length()+i));
                    //Integer.parseInt(postfix.substring(0,i));

As you can see, I have tried some different string manipulation but they're all incorrect. My supervisor said something about reading the string from backwards or the last string or something, But I never understood what they meant. Thanks for any help in advance

SARose
  • 3,558
  • 5
  • 39
  • 49
  • Does the expression `temp2.charAt(temp2.length()+i)` make sense? If there are only `temp2.length()` characters in `temp2`, what will be the index `temp2.length()+i`? – RealSkeptic Sep 19 '15 at 12:37
  • @RealSkeptic. I know it will be out of range :/. Do you have any suggestion how to make it work? – Java beginner Sep 19 '15 at 12:41
  • Well, you shouldn't be using a string at all for keeping a number. Remember that to make the number 21 from `2` and `1`. You can put put 2 in the number first, and then when you see the 1, multiply what you have by 10 and add the 1. Another hint: there should not be a loop inside the loop. – RealSkeptic Sep 19 '15 at 12:59
  • Thanks for the tips. But this is a part of the calculator. I'm reading input from user, I want it to work for every sort of expression. – Java beginner Sep 19 '15 at 13:06

2 Answers2

1
temp2.charAt(temp2.length()+i)

You are accessing a character of the string with charAt. However temp2 contains temp2.length() characters. Hence you can acces them from index 0 to temp2.length() - 1. Hence accessing the character at position temp2.length()+i is out of range... (for i > 0 !!)

Take a look at your previous one, temp2.charAt(temp2.length()-1). Here you accessed the last character of the string (at index temp2.length()-1). Any access with a greater index will result in an index out of range.

EDIT : The stop condition of your while loop is while (postfix.charAt(0) != '0'). In the loop you never change the postfix string. Hence if the condition is met (first character of postfix is not '0') you'll have an infinite loop. Hence you'll never reach the return statement.

Kevin
  • 2,813
  • 3
  • 20
  • 30
  • temp2 is integer, can't use index(0,i). Do you have any suggestion how to make it work? Thank – Java beginner Sep 19 '15 at 12:42
  • @Javabeginner No, `temp2` is a string, `String temp2 = "";`. Do you have any idea what you are doing? – Kevin Sep 19 '15 at 12:47
  • @HuperZ. oh sorry! I faced a new error thay it keeps looping. 'result' doesn't return in my main. – Java beginner Sep 19 '15 at 13:02
  • Thanks a lot man! I commented out the condition. removed delete = delete.substring(0,i);. and changed num2 to num2 = Character.getNumericValue(temp2.charAt(temp2.length()+i-1)); but this time the error says to be here, at num2. Any idea? Thanks again – Java beginner Sep 19 '15 at 13:20
  • @Javabeginner Did you read my answer? Index `temp2.length()+i-1` will be out of range **again** when `i>=1` because **there are only `temp2.length()` characters from index 0 to `temp2.length() - 1`.** – Kevin Sep 19 '15 at 13:27
  • I hate it when my code becomes a guessing game! I read your answer, and tried to implement it, but what I want the code to so is to read the integers among operators and do the math operations. tbh feel lost now :/ – Java beginner Sep 19 '15 at 13:33
  • @Javabeginner Programming isn't a guessing game. Before you start writing code you should have an idea how to solve the problem. Once you know how to solve it you can start implementing it, hence not guessing but following your idea. – Kevin Sep 19 '15 at 13:36
  • I am following the the idea and the instructions given. Anyway, how is num2 = Character.getNumericValue(temp2.charAt(temp2.length()+i-1)); out of range? Any help would be much appreciate it at this moment, it's getting frustrating with this code – Java beginner Sep 19 '15 at 13:42
  • @Javabeginner I explained why it is going out of range in an earlier comment (see bolt text). I would recommend you to edit the post to add the idea and the instructions you have to follow (i presume this is an assignment) so we can check where you are going wrong. – Kevin Sep 19 '15 at 13:54
0

Change this line Character.getNumericValue(temp2.charAt(temp2.length()+i)); to Character.getNumericValue(temp2.charAt(temp2.length()+i-1));

Ars
  • 106
  • 5