I'm working on a fraction calculator using String.split()
to get the terms split. The inputs are separated by spaces( 1/2 / 1/2)
String[] toReturn = new String[6];
result = isInputValid(expression);
toReturn = splitExpression(expression, placeToSplit[0]);
int indexOfUnderscore = toReturn[0].indexOf("_");
result = isInputValid(toReturn[0]);
if(toReturn[5] != null){
getOperator2(toReturn);
}
The error is in the if statement. toReturn[5]
is out of bounds, because when two terms or less were answered split expression, which uses String.split()
to split it at the spaces, doesn't create toReturn[5]
, even when I set values to toReturn[5]
. If there is a way to tell if a field in an array exists, that could solve it, or if there is a way to tell how many terms are being put in. My program works for 1/2 + 1/2 * 1/2
, but I haven't figured out how to tell if toReturn[5]
exists.