扫描仪异常错误?

时间:2017-12-14 10:37:26

标签: java

错误不应该发生,因为我已经关闭了我在registerN()函数中声明的Scanner对象,并声明了setInfo()要使用的新对象。但是,每当我输入"register"作为option的值时,我仍然会得到以下输出。

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at Student.setInfo(student.java:37)
    at Group.registerN(student.java:87)
    at Manage.main(student.java:168)

这是代码,

import java.util.*;
import java.io.File;
import java.io.PrintWriter;

// **** Student details ****// 
class Student {
    // Identity description
    String name;
    char sex;
    int age;
    String id;
    // Educational description
    String department;

    void setInfo(){
        Scanner input = new Scanner(System.in);

        System.out.println("Please input the following information about the student...");
        System.out.print("Name:\t");
        name = input.nextLine();
        System.out.print("Sex:\t");
        sex = Character.toLowerCase(input.next().charAt(0));
        System.out.print("Age:\t");
        age = input.nextInt();
        System.out.print("Department:\t");
        department = input.nextLine();
        System.out.print("ID:\t");
        id = input.nextLine();

        input.close();
    }

}
// **** **** **** **** **** **** //


// **** Collection of the students **** //
class Group {
    ArrayList<Student> DB = new ArrayList<Student>();

    Student temp = new Student();

    void registerN(){
        Scanner input = new Scanner(System.in);
        System.out.print("How many students would you like to register: ");
        int n = input.nextInt();
        input.close();

        for(int i = 1; i <= n; ++i){
            temp.setInfo();
            DB.add(temp);
            System.out.println("Student(s) " + i + " out of " + n + " registered.");
        }
    }
}
//**** **** **** **** **** **** //

// **** A class to make use of the objects **** //
class Manage {

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        Group base = new Group();
        // option string
        String option = "";

        // I specify the options a user can input here

        while(true){
            System.out.print("option: ");
            option = input.nextLine();
            option = option.toLowerCase();

            if(option.equals("register")){
                base.registerN();
            }
            else
                System.out.println("\t\"" + option + "\" not recognized!\n\tReview options list.");
        }
        input.close();
    }
}

我在点控件上设置了一个断点,该控件被传递给函数base.registerN();,而函数temp.setInfo()又将控制传递给setInfo()。但是在发生这种情况后立即Please input the following information about the student... Name:打印出INSERT INTO P3.CUSTOMER(ID, Name, Gender, Age, Pin) VALUES( NEXT VALUE FOR P3.THEID , NAME, GENDER, AGE, P3.ENCRYPT(PIN)); SET ID = P3.THEID.CURRVAL; ,我得到了上面的错误。那是为什么?

2 个答案:

答案 0 :(得分:1)

在下面的方法中,您通过调用input.close();

关闭了输入流
void registerN(){
        Scanner input = new Scanner(System.in);
        System.out.print("How many students would you like to register: ");
        int n = input.nextInt();
        input.close();

如果您调用方法input.close(),{}将关闭System.in。它不会接受任何进一步的输入。

如果你想避免这种情况,创建一个可供所有类使用的扫描仪的全局变量

System.in。没有被你的代码实例化,而是由JVM实例化。

JVM会在需要时关闭它。

答案 1 :(得分:0)

当您通过调用Scanner处置close()时,即使您在其周围构建了新的Scanner,也无法再从其输入流中读取。这就是为什么在Scanner方法中关闭registerN setInfo方法会导致Scanner方法内的错误。

您应该通过在代码周围传递一个setInfo对象来改变方法,而不是在void setInfo(Scanner input){ ... name = input.nextLine(); ... } 方法中构建一个“按需”:

Scanner

同时将registerN传递给if(option.equals("register")){ base.registerN(input); }

Scanner

这样您就可以使用在main中创建的单个meta-toradex,而不会将其关闭。

相关问题