title: How to exit a for loop java?
body: <p>Hi I am writing java code that will take user input and place the user input into an ArrayList but I want the user to be able to enter Q when they have input all the data. But when I input q on certain instances I am getting the wrong output.</p>
  
  <pre><code>public void enterScores() 
  {
      System.out.println("Enter the scores of the student, press Q to finish");
      for (int i = 0; i &lt; SCORES_SIZE; i++)
      {
          exitLoop = userInput.next();
          if (exitLoop.equalsIgnoreCase("Q"))
          {
              break;
          }
          scores.add(i, userInput.nextInt());
      }
      System.out.println("___________");
      for (int i = 0; i &lt; scores.size(); i++)
          System.out.println(scores.get(i)); //Prints out the arraylist of scores entered
      System.out.println("_____");
      System.out.println(scores.size()); //prints out the size of the arraylist but is wrong
  }
  </code></pre>
  
  <p>The following code after the for loop was only so I could make sure it was running properly but unfortunately it is not. It seems as if it is only reading some of the numbers into the ArrayList So the input I am getting is:</p>
  
  <pre><code>   Enter the scores of the student, press Q to finish
  12
  13
  14
  145
  14
  13
  q
  ___________
  13
  145
  13
  _____
  3
  </code></pre>
  
  <p>So when I enter q (to quit the loop) in an odd position, the program will exit the loop but the only numbers that are read into the ArrayList are the numbers in the even positions. And when i enter q in an even position I get the following error:</p>
  
  <pre><code>Exception in thread "main" java.util.InputMismatchException
      at java.util.Scanner.throwFor(Unknown Source)
      at java.util.Scanner.next(Unknown Source)
      at java.util.Scanner.nextInt(Unknown Source)
      at java.util.Scanner.nextInt(Unknown Source)
      at Chapter_7.GradeBook.enterScores(GradeBook.java:36)
      at Chapter_7.GradeBookTester.main(GradeBookTester.java:12)
  </code></pre>