title: Finding the nth occurrence of a character in a String using IndexOf()
body: <p>I have a question regarding <code>indexOf()</code>. I am trying to program an <code>EmailExtractor</code> (Yes, this is a homework but I am not looking for code) which extracts the entire email address from a sentence that is input by a user. </p>
  
  <p>For example -
  <strong>User Input:  Mail us at abc@def.ghi.jk with your queries.</strong>
  The program will then display <strong>abc@def.ghi.jk</strong> from the above <code>String</code>. I understand <code>indexOf()</code> and <code>substring()</code> are required. </p>
  
  <p>The idea I have now is to use <code>indexOf()</code> to locate the '@', and then search for the empty space just before the email address input by the user (nth). </p>
  
  <p>My code is as follows:</p>
  
  <pre><code>System.out.println("This is an Email Address Extractor.\n");
  System.out.print("Enter a line of text with email address: ");
  String emailInput = scn.nextLine();
  int spaceAt = emailInput.indexOf(" ");
  for (int i = 1; i &lt;= emailInput.indexOf("@"); i++){
      if (spaceAt &lt; emailInput.indexOf("@")) {
          spaceAt = emailInput.indexOf(" ", spaceAt + 1);
      }
  }
  </code></pre>
  
  <p>I understand and am aware of the problem in my code. </p>
  
  <p>1) <code>"Mail us at abc@def.ghi.jk with your queries".indexOf(" ")</code> is 4, I am trying to get 10. However, the IF Condition I have input will cause it to skip to the next instance of indexOf() which is 25. (Because 10 &lt; 14).</p>
  
  <p>How do I go about avoiding this from happening? </p>
  
  <p>Once again, I am not looking for purely the answer rather, I am trying to work around a solution. Thanks in advance!</p>