Q:

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 I run this program it's giving me an error: index out of bounds. How can I solve it?

A:

You're not changing a in the loop, so a.isEmpty() will not change and you'll continue looping until i goes out of bounds. Did you mean:
while (i < a.length()) {

And as an aside, a.toCharArray()[i] can (and should) simply be a.charAt(i) (as @MartijnCourteaux also points out).

