一尘不染

扫描仪NoSuchElementException

java

我的Java作业有问题。我遇到了意外的异常,特别是:

java.util.NoSuchElementException:找不到行

我正在使用Scanner(System.in),该程序不断读取任何内容,并重复“无效格式”异常文本。如果输入正确的值int,则第一部分通过,然后该double部分立即进入此异常。如果输入的值不正确int,则它将开始循环异常。

这是我的代码:

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;
    }
}

阅读 199

收藏
2020-09-08

共1个答案

一尘不染

Scanner关闭了多个库,这些库关闭了底层库InputStream,因此另一个库Scanner将不再从同一库InputStreamNoSuchElementException结果中读取。

对于控制台应用程序,请使用一个Scanner来读取System.in

2020-09-08