java - My program gives an index out of bounds error -
int i=0; while(!a.isempty()) { if(a.tochararray()[i]==' ') { system.out.println(a.tochararray()[i]); } i++; } when run program it's giving me error: index out of bounds. how can solve it?
you're not changing a in loop, a.isempty() not change , you'll continue looping until i goes out of bounds. did mean:
while (i < a.length()) { and aside, a.tochararray()[i] can (and should) a.charat(i) (as @martijncourteaux points out).
Comments
Post a Comment