Saturday, 15 August 2015

indexoutofboundsexception - Java Array Index Out of Bounds Exception Processing -



indexoutofboundsexception - Java Array Index Out of Bounds Exception Processing -

given next java codes:

int test = createintdata(column[8]); private int createintdata (string realdata) { if (realdata == null) { homecoming (int) (math.random()*100); } else { homecoming integer.parseint(realdata); } }

this throws exception this:

class="lang-none prettyprint-override">exception in thread "main" java.lang.arrayindexoutofboundsexception

this because argument have maximum index of 4. how alter programme realize function that: 1 time argument out of index, homecoming value:

math.random() * 100

i go allowing check within method, passing array , index:

public int createintdata(string[] values, int index) { if (values != null && index >= 0 && index < values.length) { homecoming integer.parseint(values[index]); } homecoming (int) (math.random()*100); }

call it:

int test = createintdata(column, 8);

this way method can safely called input (i left potential exception in parseint, though)

java indexoutofboundsexception

No comments:

Post a Comment