扫描仪 NoSuchElementException [英] Scanner NoSuchElementException

查看:41
本文介绍了扫描仪 NoSuchElementException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

java.util.NoSuchElementException:找不到行

java.util.NoSuchElementException: No line found

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

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.

这是我的代码:

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

推荐答案

您关闭了多个 Scanner,这会关闭底层 InputStream,因此另一个 Scanner 无法再从相同的 InputStreamNoSuchElementException 结果中读取.

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.

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

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

这篇关于扫描仪 NoSuchElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆