title: Write code to print the location of any space in string
body: <p>So I'm trying to write code to print the location of any space in the 2-character string passCode. Each space detected should print a separate statement followed by a newline.</p>
  
  <p>i wrote this code and it would work until passcode = " "; </p>
  
  <pre><code>  public class space{
    public static void main(String args[]) { 
    String passCode = "";
    passCode = "A ";
  
    int p = passCode.length();
  
    /*
    for(int i = 0; i &lt; p; i++) {
       int b = passCode.charAt(i);
       if(Character.isWhitespace(b)){
          System.out.println("Space at " + passCode.indexOf(b));
       }
    }
    } 
    } 
  </code></pre>
  
  <p>however i found a way of doing it where it will evaluate correctly if passcode = " "; 
  posted below </p>
  
  <pre><code>int indexOfSpace = passCode.indexOf(" ");
  
     while(indexOfSpace &gt;= 0){
  
       System.out.println("Space at " + indexOfSpace);
       indexOfSpace = passCode.indexOf(" ", indexOfSpace + 1);
  
       }
  </code></pre>
  
  <p>could someone please explain to me why it works when both indexes are blank for the while loop and not the for loop ?</p>
  
  <p>Thank you </p>