Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组索引出边界帮助(Java)_Java_Arrays_Error Handling_Indexoutofboundsexception - Fatal编程技术网

数组索引出边界帮助(Java)

数组索引出边界帮助(Java),java,arrays,error-handling,indexoutofboundsexception,Java,Arrays,Error Handling,Indexoutofboundsexception,这个Java程序执行一个简单的计算,假设它输出123+1 (输出应为124。)(忽略最后一个“+”字符串。) if语句中有一个错误: 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:4 我为arrayz[I-1]和arrayz[I+1]做了一个打印输出,似乎可以接受地打印出123和1,这是正确的。所以,我不确定出了什么问题 String math = "123+1+"; String arrayz[]={"

这个Java程序执行一个简单的计算,假设它输出
123+1

(输出应为124。)(忽略最后一个“+”字符串。)

if语句中有一个错误:

线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:4

我为
arrayz[I-1]
arrayz[I+1]
做了一个打印输出,似乎可以接受地打印出123和1,这是正确的。所以,我不确定出了什么问题

        String math = "123+1+";
        String arrayz[]={"123","+","1","+"};
        double total =0.0;

        int i=0;
        while(i<=(math.length()-1))        //don't bother with the last char
        {       i++;
            if(arrayz[i].equals("+"))
            {
                total = Double.parseDouble((String)arrayz[i-1]) + Double.parseDouble((String)arrayz[i+1]);

            }

        }

        System.out.println(total);
String math=“123+1+”;
字符串arrayz[]={“123”、“+”、“1”、“+”};
双倍合计=0.0;
int i=0;

(i由于您将索引
i
用于数组
arrayz
,因此必须使用
arrayz.length
而不是
math.length()

编辑

这应该起作用:

public static void main(String[] args)
{
    String math = "123+1+";
    String arrayz[] = { "123", "+", "1", "+" };
    double total = 0.0;

    int i = 0;

    // arrayz.length - 2 -> because you are accessing "arrayz[i + 1]", 
    // arrayz.length - 1 would be OK if the maximum index you were using were "arrayz[i] "
    while (i <= (arrayz.length - 2)) //don't bother with the last char
    {           
        if (arrayz[i].equals("+")) {
            total = Double.parseDouble((String) arrayz[i - 1]) + Double.parseDouble((String) arrayz[i + 1]);
        }
        i++; // Increment at the end of the loop
    }

    System.out.println(total);
}
publicstaticvoidmain(字符串[]args)
{
字符串数学=“123+1+”;
字符串arrayz[]={“123”、“+”、“1”、“+”};
双倍合计=0.0;
int i=0;
//arrayz.length-2->因为您正在访问“arrayz[i+1]”,
//如果使用的最大索引是“arrayz[i]”,那么arrayz.length-1就可以了

while(i
while(i您正在循环字符串
math
,同时访问循环中
arrayz
的元素,并认为它们具有相同的元素和长度。

我建议您使用字符串而不是数学字符串(在本例中您可以省略它,但我假设您在某些条件下通常不能),您可以使用字符串类型的数组,这样您的示例中的123将是第一个元素
arrayz[0]

尝试使用
长度
而不使用
()
没门!有什么错误?在所有的计算之后,换句话说,在循环结束时,您可以增加(i++)。您可以使用for循环来避免这种情况。当我在(我所发生的是,您正在使用索引
i+1
进行访问,您也必须更正此问题。是的,我将i++放在if语句之后。我仍然在循环的末尾而不是开始处获取ArrayIndexOutOfBoundsException错误增量i。
   while(i<=(math.length()-1))  
 while(i<=(arrayz.length-1))