title: Using Scanner inside a for loop for system input
body: <p>I have been struggling with this for a while. I essentially want to loop through and read in as many strings  as determined by <code>num_choices</code>. The following code only executes the else condition.  </p>
  
  <pre><code>        Scanner s2 = new Scanner(System.in);
  
          for(int i=0; i &lt; this.num_choices; i++)
          {
            if(s2.hasNext())
            {
  
              System.out.println("Enter choice " + (i+1) +":");
              String ch = s2.next();
              //this.choices.addElement(ch);
            }
          else
          {
              System.out.println("Lets end this");
  
          }
  
      }
  </code></pre>
  
  <p>`</p>
  
  <p>I am getting this: <code>Exception in thread "main" java.util.NoSuchElementException</code>. In the main class, this is where the error points to </p>
  
  <pre><code>        choice2 = Integer.parseInt(read_choice2.next());
  </code></pre>
  
  <p>which is inside a while loop as well. Here is the code for that:</p>
  
  <pre><code>public class Main 
  {
  public static void main(String args[]) throws IOException
  {
      Vector&lt;Survey&gt; mysurveys = new Vector&lt;Survey&gt;();
      boolean carry_on = true;
      int choice = 0;
      Scanner read_choice = new Scanner(System.in);
  
      System.out.println("Let's begin the Survey/Test application!");
      while(carry_on)
      {
          System.out.println("What would you like to do?");
          System.out.println("1. Create a new Survey");
          System.out.println("2. Create a new Test");
          System.out.println("3. Display a Survey");
          System.out.println("4. Display a Test");
          System.out.println("5. Save a Survey");
          System.out.println("6. Save a Test");
          System.out.println("7. Load a Survey");
          System.out.println("8. Load a Test");
          System.out.println("9. Quit");
          System.out.println();
          System.out.println("Please enter a number for the operation you want to perform: ");
          choice = Integer.parseInt(read_choice.next());
          /*try
          {
              choice = Integer.parseInt(buffer.readLine());
  
          }
          catch(InputMismatchException  e)
          {
              System.out.println("Invalid input. Please Enter again.");
              System.out.println();
              //read_choice.nextInt();
          }*/
  
          switch(choice)
          {
          case 1:
              System.out.println("Please Enter a Name for your Survey");
              String in = buffer.readLine();
              Survey s1 = new Survey();
              s1.CreateNew(in);
              mysurveys.add(s1);
              ////
              add_question(s1.type);
              break;
  
          case 2:
              System.out.println("Please Enter a Name for your Test");
              //String in = buffer.readLine();
              Test t1 = new Test();
              //t1.CreateNew(in);
              mysurveys.add(t1);
              break;
              ////
              //add_question(t1.type);
          case 3:
  
  
              break;
              // call Survey.display()
  
  
          case 4:
  
  
              break;
  
  
          case 5:
  
              Survey s = new Survey();
              ReadWriteFiles x = new ReadWriteFiles();
              x.SaveSurvey(s);
              break;
  
  
          case 6:
  
              Test t = new Test();
              //ReadWriteFiles x = new ReadWriteFiles();
              //x.SaveSurvey(t);
              break;
  
  
          case 7:
  
              carry_on = false;
              break;
  
          default:
  
              System.out.println("Incorrect Input. Try Again");
              System.out.println();
              break;
          }
      }
  
      read_choice.close();
  
  
  }
  
  
  
  
  
  
  
   public static void add_question(String type) throws IOException, NullPointerException
   {
  Questions q = null;
  boolean carry_on2 = true;
  int choice2 = 0;
  Scanner read_choice2 = new Scanner(System.in);
  //BufferedReader buffer2=new BufferedReader(new InputStreamReader(System.in));
  
  while (carry_on2)
  {
      //
      System.out.println("1. Add a new T/F Question");
      System.out.println("2. Add a new Multiple Choice Question");
      System.out.println("3. Add a new Short Answer Question");
      System.out.println("4. Add a new Essay Question");
      System.out.println("5. Add a new Ranking Question");
      System.out.println("6. Add a new Matching Question");
      System.out.println("7. If you want to stop adding more questions, and go back to the main menu.");
      System.out.println("Please enter a number for the operation you want to perform: ");
      choice2 = Integer.parseInt(read_choice2.next());
  
      /*try
      {
          choice2 = Integer.parseInt(buffer2.readLine());
  
      }
      catch(InputMismatchException  e)
      {
          System.out.println("Invalid input. Please Enter again.");
          System.out.println();
          //read_choice2.nextInt();
      }*/
      switch(choice2)
      {
          case 1:
              q = new TrueFalse();
              break;
          case 2:
              q = new MultipleChoice();
              break;
          case 3:
              q = new ShortAnswer();
              break;
          case 4:
              q = new Essay();
              break;
          case 5:
              q = new Ranking();
              break;
          case 6:
              q = new Matching();
              break;
          case 7:
              carry_on2 = false;
              break;
          default:
              System.out.println("Incorrect Input.");
              break;
  
      }
      q.createQuestion(type);
  
  }
  }
  }
  </code></pre>
  
  <p>I realize there is a lot of messy code, and I apologize for that. I just wanted to show the entire thing, so it's easier to spot the problem. Help would be appreciated.</p>