title: Scanner NoSuchElementException
body: <p>I'm having a problem with my Java assignment. I'm getting an unexpected exception, specifically: </p>
  
  <blockquote>
    <p>java.util.NoSuchElementException: No line found</p>
  </blockquote>
  
  <p>I am using <code>Scanner(System.in)</code> and the program is continually reading nothing and repeating the "invalid format" exception text. If I enter a correctly valued <code>int</code>, the first part passes and then the <code>double</code> part immediately goes into this exception. If I enter an incorrectly valued <code>int</code>, then it starts looping the exception. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Program_4 {
  
      public static void main(String[] args) {
          getValidInt("Enter an integer from 5 to 50",5,50);
          getValidDouble("Enter a double from 5.0 to 50.0",5.0,50.0);
          getValidString("Enter a string with length from 5 to 8 characters",5,8);
      }
  
  
      public static int getInt(String prompt)
      {
         Scanner sc = new Scanner(System.in);
         int i = 0;
         boolean isValid;
         do
         {
            try
            {
               System.out.print(prompt + ": ");
               i = Integer.parseInt(sc.nextLine());
               isValid = true;
            } 
            catch (Exception e)
            {
                System.out.println(e);
                System.out.print("Invalid Format: ");
                isValid = false;
            }
         }
         while (isValid == false);
         sc.close();
         return i;
      }
  
      public static int getValidInt(String prompt, int min, int max)
      {
          int i = 0;
          boolean isValid = false;
          do
          {
              i = getInt(prompt);
              if(i &lt; min) System.out.println("Value must be &gt;= " + min);
              else if(i &gt; max) System.out.println("Value must be &lt;= " + max);
              else isValid = true;
          } while (isValid == false);
  
          return i;
      }
  
      public static double getDouble(String prompt)
      {
         Scanner sc = new Scanner(System.in);
         double i = 0.0;
         boolean isValid;
         do
         {
            try
            {
               System.out.print(prompt + ": ");
               i = Double.parseDouble(sc.nextLine());
               isValid = true;
            } 
            catch (Exception e)
            {
                System.out.println(e);
                System.out.println("Invalid Format: ");
                isValid = false;
            }
         } while (isValid == false);
         sc.close();
         return i;
      }
  
      public static double getValidDouble(String prompt, double min, double max)
      {
          int i = 0;   
          boolean isValid = false;
          do
          {
              i = getInt(prompt);
              if(i &lt; min) System.out.println("Value must be &gt;= " + min);
              else if(i &gt; max) System.out.println("Value must be &lt;= " + max);
              else isValid = true;
          } while (isValid == false);
  
          return i;
      }
  
      public static String getString(String prompt)
      {
         Scanner sc = new Scanner(System.in);
         String i="";
         boolean isValid;
         do
         {
            try
            {
               System.out.print(prompt + ": ");
               i = sc.nextLine();
               isValid = true;
            } 
            catch (Exception e)
            {
                System.out.print("Invalid Format: ");
                isValid = false;
            }
         } while (isValid == false);
         sc.close();
         return i;
      }
  
      public static String getValidString(String prompt, int min, int max)
      {
          String i;
          boolean isValid = false;
  
          do
          {
              i = getString(prompt);
              if(i.length() &lt; min) System.out.println("String must be more than "       + min + " characters.");
              else if(i.length() &gt; max) System.out.println("String must be more     than " + max + " characters.");
              else isValid = true;
          } while (isValid == false);
  
          return i;
      }
  }
  </code></pre>