Array Index Out Of Bounds - Java

故事扮演 提交于 2019-12-02 03:45:59

When operationIndex is equal to the last element index in outputNum, then operationIndex + 1 will be greater than the last element index: hence your exception

You would be better off using a for loop that started at index 1, and doing:

// assuming that operationList and outputNum always 
//   have the same number of elements
for (int i = 1; i < operationList.size(); i++) {
  ...
  answer = outputNum.get(i - 1) * outputNum.get(i);
  ...
}

or, alternatively, use your current loop, but do:

  // operationIndex++; // remove this and use ..
  if (++operationIndex >= outputNum.size()) break;

You are only ever adding one item to your list:

currentNum = Double.parseDouble(currentString);
outputNum.add(currentNum);

Then you try to access what seems to be a second object at index operationindex +1 which doesn't exist:

answer = outputNum.get(operationIndex) * outputNum.get(operationIndex+1);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!