Okay. So I'm trying to write an applicaton that solves problems in the correct order of operations (i.e. PEMDAS, but without the PE lol). I'm probably doing this wrong, but whatever. What I need help on is that as a iterate through each element in the array, if it is an operator, I remove two elements(the numbers surrounding the operator because I replace the operator the correct computation, so an array with three elements, 5+5, will become an array with one element, 10) which would be fine, but the for-statement condition doesn't update, so, in other words, when i update the array, the array.length gets shorter, but the for-statement doesn't recognize that and continues on pass the bounds of the array, this causes outofbounds errors. Best way to explain is by my code, so here it is:
for (i = 0; i < splitEquation.length; i++){
if (splitEquation[i].equals("*")){
splitEquation[i] = Double.toString(Double.parseDouble(splitEquation[i - 1]) * Double.parseDouble(splitEquation[i + 1]));
splitEquation = removeIndex(splitEquation, i - 1);
splitEquation = removeIndex(splitEquation, i + 1);
solution += Double.parseDouble(splitEquation[i - 1]);
}
}
And removeIndex():
private String[] removeIndex(String [] s, int index){
String [] n = new String[s.length - 1];
System.arraycopy(s, 0, n, 0, index - 1);
System.arraycopy(s, index, n, index - 1, s.length - index);
return n;
}
Thanks in advance, -Eric
P.S. Let me know if you need any clarification of what my code does :)