title: Remove spaces from a user input String
body: <p>I'm trying to create a code to remove spaces in a user input String, but I receive an error at line 16. </p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
    15 at practice5.main(practice5.java:16)</p>
  </blockquote>
  
  <p>Note: We are not allowed to use the replace method of Java. I need to code it manually. </p>
  
  <p>Here's my code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class practice5{
      public static void main(String args[]){
          Scanner scanner = new Scanner(System.in);
          String line;
          System.out.print("Sentence: ");
          line = scanner.nextLine();
          char phrase[] = line.toCharArray();
          int i = 0;
          int n = 0;
  
          while(phrase[i] != '\0') {
              if(phrase[i] == ' ') {
                  for(n=i; n&lt;phrase.length; n++) {
                      phrase[n] = phrase[n+1];
                  }
              }
              i++;
          }
  
          String output = new String(phrase);
          System.out.print(output);
      }
  }
  </code></pre>
  
  <p>Thank you!</p>