public static float getSelectedHeight() {
String selectedHeightValue = (String)heightSpin.getSelectedItem();
if (heightSpin.getSelectedItemPosition() == 0 ) {
String feets = selectedHeightValue.substring(0,1);
String inches = selectedHeightValue.substring(2,4);
return (float) (Float.parseFloat(feets) * 0.3048) + (float) (Float.parseFloat(inches) * 0.0254);
} else {
return Float.parseFloat(selectedHeightValue);
}
}
Error String index out of bound exception i don't know how to solve it.. Please Help me in solving this issue
**java.lang.StringIndexOutOfBoundsException: length=2; regionStart=2; regionLength=2**
The problem might be the lines with selectedHeightValue.substring(...)
To solve it, first of all as a general rumb of thumb, before doing any operation with substring, you should check selectedHeightValue must be valid (so not NULL and not EMPTY).
And then make sure your string size
is enough to make a substring of it (so that it does not exceed its size when you try to access a position in the string that doesn't exist).
Exmple
: the string "ABC" has size 3. And have positions: 0,1,2. If you try to access the string "ABC" in position 3 and up (4,5,6...) than I guess is gonna throw an error because you want to access something that doesn't exist.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments