9

I'm having a problem with my Java assignment. I'm getting an unexpected exception, specifically:

java.util.NoSuchElementException: No line found

I am using Scanner(System.in) and the program is continually reading nothing and repeating the "invalid format" exception text. If I enter a correctly valued int, the first part passes and then the double part immediately goes into this exception. If I enter an incorrectly valued int, then it starts looping the exception.

Here is my 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 < min) System.out.println("Value must be >= " + min);
            else if(i > max) System.out.println("Value must be <= " + 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 < min) System.out.println("Value must be >= " + min);
            else if(i > max) System.out.println("Value must be <= " + 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() < min) System.out.println("String must be more than "       + min + " characters.");
            else if(i.length() > max) System.out.println("String must be more     than " + max + " characters.");
            else isValid = true;
        } while (isValid == false);

        return i;
    }
}
syb0rg
  • 7,747
  • 7
  • 38
  • 77
user2175782
  • 95
  • 1
  • 1
  • 3

2 Answers2

19

You have more than one Scanner that you close, which closes the underlying InputStream, therefore another Scanner can no longer read from the same InputStream and a NoSuchElementException results.

For console apps, use a single Scanner to read from System.in.

syb0rg
  • 7,747
  • 7
  • 38
  • 77
  • Thanks for the help, eclipse was giving me a warning about not closing scanner and I wouldn't have added that otherwise. – user2175782 Mar 15 '13 at 23:19
3

Since you print out the same message in all three places where an exception is caught, it is difficult to say with any certainty what is going on:

  • Use printStackTrace() to find out where the exception is happening

  • Don't catch Exception like that. Catch the exceptions that you are expecting and that your code is designed to handle. If you catch Exception you could end up catching all sorts of unexpected exceptions (NPE, end of file, etc) ... and incorrectly reporting them as "Invalid format".

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • 1
    +1. Also, in `getValidDouble(String prompt, double min, double max)` you have `int i = 0;`, are you sure you want i to be an `int`? – Adeeb Mar 15 '13 at 23:10
  • Please explain the downvote. Note that the original version of the question did not say what the exception was. – Stephen C Mar 16 '13 at 01:26