我没有得到这样的元素异常:运行项目时找不到文件

时间:2018-07-29 14:14:32

标签: java

在注册方法时出错,请有人对其进行调试。我已经尝试了所有方法,但仍然遇到此异常:找不到文件。我认为我已经正确地执行了此功能。该功能应该能够获得数学101,英语101之类的课程,并相应地向我收取学费。

package studentDatabaseApp;

import java.util.Scanner;

public class Student {

    private String firstName;
    private String lastName; 
    private int gradeYear = 0;
    private String studentID;
    private String courses = null;
    private static int courseCost = 600;
    private int tutionBalance = 0;
    private static int id = 1000;

    //Constructor to enter student name and year for each student
    public Student() {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Student First Name: ");
        this.firstName = scan.nextLine();
        System.out.print("Enter Student Last Name: ");
        this.lastName = scan.nextLine();
        System.out.print("1 - Freshmen\n2 - Sophmore\n3 - Junior\n4 - Senior\nEnter Student Grade Year: ");
        this.gradeYear = scan.nextInt();
        scan.close();
        //Setting student id
        setStudentID();
        System.out.println("STUDENT CREATED: "+ firstName +" "+ lastName +" "+"\nGRADE LEVEL:"+" "+gradeYear+ " "
        +"\nSTUDENT ID: "+ studentID);                      
    }
    //Unique id and student grade level

    private void setStudentID() {
        id++;
        this.studentID = gradeYear + "" + id;
    }

    //Create courses so student can enroll
    public void enroll() {
        do {            
            System.out.print("Enter course to enroll (Q to Quit): ");
            Scanner in = new Scanner(System.in);
            String course = in.nextLine();
            if(!course.equals("Q")) {
                courses = courses + "\n" + course;
                tutionBalance = tutionBalance + courseCost;
            } else {
                System.out.println("Break");
                break;
            }
            in.close();         
        } while(1 != 0);

        System.out.println("ENROLLED IN: "+ courses);
        System.out.println("TUTION BALANCE: "+ tutionBalance);
    }

    //Student should able to view their balance and pay the tution 

    //Student status with their name, ID, course enrolled and balance

}

1 个答案:

答案 0 :(得分:2)

第一次关闭扫描仪时,也会同时关闭System.in,因此当您第二次尝试访问System.in中的enroll()时会遇到异常,请尝试重构代码,以便在班上只能使用一台扫描仪。