title: I get a java.util.NoSuchElementException on input from System.in
body: <p>I am writing code for a program which displays a map based a file with x &amp; y coordinates, as well as a type value.</p>
  
  <p>My code correctly displays the ASCII map and allows me to perform functions on the map, but when the code finally returns to the switch statement where it should take input for choosing the next option, it skips the line "option = input.nextInt();" and continues to the finally block where "input.nextLine();" gives me a Java.util.NoSuchElementException.</p>
  
  <p>Does anyone know why this is? I know that this exception usually occurs with an enumeration when there is no such element, but I don't know why my code would skip the input.nextInt() line and then fail on input.nextLine();</p>
  
  <p>This error occurs only when the Gorgon case is activated in the viewMap Switch case at the end. I have tried adding "input.nextLine()" calls before the "input.nextInt()" call, but it doesn't make a difference.</p>
  
  <p>Included is the code for the Switch Statement in my MapViewer program, as well as the code for building the map and displaying it. I have also added the Gorgon class at the end.</p>
  
  <pre><code>public class MapViewerMenu {
  
      public int option = 0;
      public boolean complete = false;
  
      Scanner input = new Scanner(System.in);
  
      char [][] mapper = null;
  
      Map currentMap = null;
      User currentUser = null;
      Vector &lt;Grue&gt; currentGrues = null;
  
      public void run(){
  
          while(!complete){
              System.out.println("*******************");
              System.out.println("* Map Viewer Menu *");
              System.out.println("*******************");
              System.out.println("1. Load Files");
              System.out.println("2. Set Symbols");
              System.out.println("3. View Map");
              System.out.println("4. Scramble Map");
              System.out.println("5. Reset Map");
              System.out.println("6. Exit");
              System.out.println("");
  
              try{
                  option = input.nextInt();
              }
              catch(Exception e){
                  System.out.println("Error in input.");
                  System.out.println("Try again.");
                  System.out.println("");
              }
              finally{
  
  // The Error occurs once the program exits viewMap and returns here. 
  //It skips the above "option = input.nextInt();" and comes down here and fails.
  
                  input.nextLine();
              }
  
              switch(option){
  
              //removed irrelevant cases
  
              case 3:
                  viewMap();
                  break;
  
              case 6:
                  complete = true;
                  input.close();
                  break;
  
              }
          }
      }
  </code></pre>
  
  <hr>
  
  <pre><code>    public void viewMap(){
          //removed earlier code that built part of the map for readability
  
          number = currentGrues.elements(); //this is an enumeration
  
          while(number.hasMoreElements()){
              temp3 = number.nextElement();
              int sure =2;
  
              switch(temp3.getName()){
  
              case "Gorgon":
  
                  while(sure != 0){
                      System.out.printf("Would you like the Gorgon to change a square type?\n");
                      System.out.println("(0 for yes, 1 for no.)");
  
                      try{
                          sure = input.nextInt();
                      }
                      catch(Exception e){
                          System.out.println("Error in input.");
                          System.out.println("Try again.");
                          System.out.println("");
                          sure = 2;
                      }
                      finally{
                          input.nextLine();
                      }
  
                      if(sure == 0){
                          Gorgon gor = (Gorgon) temp3;
                          gor.boulder(currentMap, mapper);
                          after = true;
                          break;
                      }
                      else{
                          if(sure == 1){
                              break;
                          }
                      }
                  }
                  break;
  
  // A boolean value after tells the program to reprint the map.
  
          if(after){
              System.out.println("Since some squares were changed, the updated map is printed.");
              System.out.println("");
              System.out.printf("Map Name: %s", currentMap.getName());
              System.out.println("");
  
              for(xpos = 0, ypos = 0; xpos &lt; 16 &amp;&amp; ypos &lt; 16;){
                  System.out.print(mapper[xpos][ypos]);
                  xpos++;
  
                  if(xpos &lt; 16){
                      continue;
                  }
                  else{
                      System.out.println("");
                      ypos++;
                      if(ypos &lt; 16){
                          xpos = 0;
                          continue;
                      }
                  }
              }
              System.out.println("");
          }
  }
  
  //This should then go back to the "option = input.nextInt" line, where it should ask the user for input, but doesnt.
  </code></pre>
  
  <hr>
  
  <pre><code>import java.util.Scanner;
  
  public class Gorgon extends Giant {
  
       //Included is the function called by the viewMap switch statement.
       //I added this because the error might exist here.
       //other classes have similar functions, but perform just fine.
  
      public void boulder(Map map, char[][] mapper){
          int x = 0;
          int y = 0;
          Scanner input = new Scanner(System.in);
  
          System.out.println("The Gorgon wants to turn an adjacent Square to stone!");
          System.out.printf("Its position is (%d,%d).\n", this.currPos.col, this.currPos.row);
  
          do{
              do{ // Get an adjacent x coordinate
                  try{
                      System.out.println("Enter an x coordinate:");
                      x = input.nextInt();
                  }
                  catch(Exception e){
                      System.out.println("Error in input.");
                      System.out.println("Try again:");
                      System.out.println("");
                  }
                  finally{
                      input.nextLine();
                  }
              }while(!(x &gt;= currPos.col-1 &amp;&amp; x &lt;= currPos.col+1));
  
              do{  // get an adjacent y coordinate
                  try{
                      System.out.println("Enter a y coordinate:");
                      y = input.nextInt();
                  }
                  catch(Exception e){
                      System.out.println("Error in input.");
                      System.out.println("Try again:");
                      System.out.println("");
                  }
                  finally{
                      input.nextLine();
                  }
              }while(!(y &gt;= currPos.row-1 &amp;&amp; y &lt;= currPos.row+1)); // keep asking for input while the input isn't within range.
  
          }while(!((x &gt;= currPos.col-1 &amp;&amp; x &lt;= currPos.col+1) &amp;&amp; (y &gt;= currPos.row-1 &amp;&amp; y &lt;= currPos.row+1)));
  
          mapper[x][y] = 'B'; //Change the value in map data.
          input.close();  //Close local input reader.
  </code></pre>
  
  <h2>        }</h2>
  
  <p>The user is at (3,3)</p>
  
  <p>I have in my collection of grues a Gorgon at (6,6) which should be able to turn an adjacent square to type "boulder", which is an obstacle the user can't stand on. </p>
  
  <p>Everytime I choose to use the Gorgon's function boulder, my program fails when it reaches the original switch statement at the beginning of the program.</p>
  
  <p><strong>When the program runs, this is how is usually goes:</strong></p>
  
  <p><strong>I load the appropriate files (these don't have to do with the error.)</strong></p>
  
  <hr>
  
  <ul>
  <li><p>Map Viewer Menu *</p>
  
  <hr>
  
  <ol>
  <li>Load Files</li>
  <li>Set Symbols</li>
  <li>View Map</li>
  <li>Scramble Map</li>
  <li>Reset Map</li>
  <li>Exit</li>
  </ol></li>
  </ul>
  
  <p>3</p>
  
  <p>Building map...</p>
  
  <p><strong>The map is then displayed with ASCII characters.</strong></p>
  
  <p>The user's Rope has been stolen!</p>
  
  <p>Would you like the Giant to change a square type?</p>
  
  <p>(0 for yes, 1 for no.)</p>
  
  <p>1</p>
  
  <p>Meat! Meat! Meat!</p>
  
  <p>Would you like the Gorgon to change a square type?</p>
  
  <p>(0 for yes, 1 for no.)</p>
  
  <p>0</p>
  
  <p>The Gorgon wants to turn an adjacent Square to stone!</p>
  
  <p>Its position is (6,6).</p>
  
  <p>Enter an x coordinate:</p>
  
  <p>7</p>
  
  <p>Enter a y coordinate:</p>
  
  <p>6</p>
  
  <p><strong>Since some squares were changed, the updated map is printed.</strong></p>
  
  <p><strong>The updated is then correctly printed again.</strong></p>
  
  <hr>
  
  <ul>
  <li><p>Map Viewer Menu *</p>
  
  <hr>
  
  <ol>
  <li>Load Files</li>
  <li>Set Symbols</li>
  <li>View Map</li>
  <li>Scramble Map</li>
  <li>Reset Map</li>
  <li>Exit</li>
  </ol></li>
  </ul>
  
  <p>Error in input.
  Try again.</p>
  
  <pre><code>Exception in thread "main" java.util.NoSuchElementException: No line found
      at java.util.Scanner.nextLine(Unknown Source)
      at viewer.MapViewerMenu.run(MapViewerMenu.java:92) //points to the finally block at the beginning, where "input.nextLine()" is written.
      at viewer.MapViewer.main(MapViewer.java:40) //This is just the original main that runs the program.
  </code></pre>
  
  <p><strong>It fails once it returns to the class's original switch statement, but it should just return to the beginning and ask for input for the switch again.</strong></p>