1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
maliddle
  • 148
  • 8

2 Answers2

5

Correctly:

result = isInputValid(expression);

String[] toReturn = splitExpression(expression, placeToSplit[0]);
int indexOfUnderscore = toReturn[0].indexOf("_");
result = isInputValid(toReturn[0]);

if(toReturn.length>5 && !"".equals(toReturn[5]) ){
    getOperator2(toReturn);
}

the toReturn.length>5 part verifies that the array itself is at least 6 items long. Then you can check if that element is empty or not...

ppeterka
  • 20,583
  • 6
  • 63
  • 78
  • 1
    Can't be null, but could be String.Empty – Francis P Oct 20 '12 at 21:31
  • 1
    also remove `String[] toReturn = new String[6]; ` and update `String[] toReturn = splitExpression(expression, placeToSplit[0]);` – Jimmy Oct 20 '12 at 21:34
  • Also I don't get what the `result = isInputValid(expression);` does, because `result` is overwritten without reading... Does it throw an exception is the String is invalid? – ppeterka Oct 20 '12 at 21:38
  • +1 for the solution. However,my code did not compile with String.Empty so I had to add another answer. – Jimmy Oct 20 '12 at 21:41
  • 1
    Java doesn't have a String.Empty representation. Some people compares against a `new String()` or a `String s = ""` but actually using `isEmpty()` is OK since it checks if the length of the string is 0. This topic [has been discussed](http://stackoverflow.com/questions/3450604/why-is-there-no-string-empty-in-java) before – Fritz Oct 20 '12 at 21:45
  • @Gamb: the funny thing is that someone else edited that into my code :) Now I put in the method how I usually check string equality in a null-safe way... (even though this can't be null this time) – ppeterka Oct 20 '12 at 21:49
  • @ppeterka I noticed that in the revision history :) – Fritz Oct 20 '12 at 21:50
0

This is what it should be like. Remove first line , String[] toReturn = new String[6];

update your third line,

String[] toReturn = splitExpression(expression, placeToSplit[0]); 

And check this condition:

if(toReturn.length>5 ){ // use !toReturn[5].isEmpty()  to check the empty string
    getOperator2(toReturn);         
    }
Jimmy
  • 2,589
  • 21
  • 31