访问字符串中的字符时,字符串索引超出范围 [英] String index out of range when accessing chars in a String

查看:573
本文介绍了访问字符串中的字符时,字符串索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作RPN计算器.我已经完成了从中缀到后缀的转换,现在我想评估转换后的表达式.输入任何表达式时都会出错

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

字符串索引超出范围:1.

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;
}

我在这部分得到错误:

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

推荐答案

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

您正在使用charAt访问字符串的字符.但是,temp2包含temp2.length()个字符.因此,您可以从索引0到temp2.length() - 1访问它们.因此,访问位置temp2.length()+i处的字符超出了范围...(对于i > 0!)

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 !!)

看看上一个temp2.charAt(temp2.length()-1). 在这里,您访问了字符串的最后一个字符(在索引temp2.length()-1).任何具有更大索引的访问都将导致索引超出范围.

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.

while循环的停止条件为while (postfix.charAt(0) != '0').在循环中,您永远不会更改postfix字符串.因此,如果满足条件(postfix的第一个字符不为"0"),则将发生无限循环.因此,您永远不会到达return语句.

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.

这篇关于访问字符串中的字符时,字符串索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆