title: Enter Integers into an Array and terminating input before reaching limit
body: <p>I'm doing a practice question in my textbook to add integers (negative and positive) into an array. I want the user to be able to terminate entering numbers into the array before it reaches the end [50]. </p>
  
  <p>This is what I've come up with:</p>
  
  <p>The user enters the numbers which is stored in a string. If keepLooping is true and index &lt; size of the array; it will parse token by token the string and place the number into the int array.</p>
  
  <p>There must be an easier way to do this and I can't get my code working, any help would be much appreciated:</p>
  
  <pre><code>// Create Objects to use in program
      Scanner keyboard = new Scanner(System.in);
      int[] arrayOfNumbers = new int[50];
  
  
      // Prompt for input
      System.out.println("Enter upto 50 integers separated by a space.");
      System.out.println("Type x to signal no more integers to enter.");
  
      int index = 0;
      boolean keepLooping = true;
  
      while (index &lt; arrayOfNumbers.length &amp;&amp; keepLooping) {
          String numToAdd = keyboard.nextLine();
  
          if ( numToAdd.equals("x") || numToAdd.equals("X") ) {
              keepLooping = false;
          }
  
          if ( !numToAdd.equals("x") || !numToAdd.equals("X") ) {
              arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
          }
      }
  
      // DEBUG, Print Array
      for (int k=0; k &lt; arrayOfNumbers.length; k++) {
          System.out.println(arrayOfNumbers[k]);
      }
  </code></pre>