Given the following Java codes:
int test = createIntData(Column[8]);
private int createIntData (String realData) {
if (realData == null) {
return (int) (Math.random()*100);
}
else {
return Integer.parseInt(realData);
}
}
This throws exception like this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
This is because the argument only have an maximum index of 4. Then how to change the program to realize the function that: once the argument is out of index, return the value:
Math.random() * 100
Column[8]. It's got nothing to do with yourcreateIntDatamethod, really. The argument is evaluated before the method gets called - so if evaluating the argument fails (as it is doing here), there's nothing the method can do about it.Column[8]andArrayIndexOutOfBoundsException: 10doesn't fit