Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
244 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...