I am trying to read from a file but I keep getting this error when running the d
ID: 3655485 • Letter: I
Question
I am trying to read from a file but I keep getting this error when running the driver for my program:
TestDriver2.java:8: error: unreported exception EmptyFileException; must be caught or declared to be thrown
University ufl = University.readFromFile("university.txt");
It is due soon and would really like some help if anyone can! Thanks.
This is the main method for reading from the file and after it is my EmptyFileException as well as the driver.
//read from file
public static University readFromFile(String filename) throws EmptyFileException {
File file = new File(filename);
University university = null;
Scanner in = null;
try {
in = new Scanner(file);
String str = in.nextLine();
Scanner in2 = new Scanner(str).useDelimiter(":");
in2.next();
String uName = in2.next();
//Read University info
str = in.nextLine();
in2 = new Scanner(str).useDelimiter(":");
in2.next();
String uTerm = in2.next();
str = in.nextLine();
in2 = new Scanner(str).useDelimiter(":");
in2.next();
int uYear = in2.nextInt();
str = in.nextLine();
in2 = new Scanner(str).useDelimiter(":");
in2.next();
int uNumStudents = in2.nextInt();
str = in.nextLine();
in2 = new Scanner(str).useDelimiter(":");
in2.next();
int uNumInstructors = in2.nextInt();
str = in.nextLine();
in2 = new Scanner(str).useDelimiter(":");
in2.next();
int uNumCourses = in2.nextInt();
//Create and read arrays of objects
Student[] uStudents = new Student[uNumStudents];
for(int i = 0; i < uNumStudents; i++)
uStudents[i] = readStudent(in);
Instructor[] uInstructors = new Instructor[uNumInstructors];
for(int i = 0; i < uNumInstructors; i++)
uInstructors[i] = readInstructor(in);
//Create array of possible TAs to pass into readCourse method
GradStudent[] gStudents = new GradStudent[uNumStudents];
for(int j=0; j<uStudents.length; j++) {
for(int i=0; i<uStudents.length; i++) {
if(uStudents[i] instanceof GradStudent)
gStudents[j] = (GradStudent) uStudents[i];
}
}
Course[] uCourses = new Course[uNumCourses];
for(int i = 0; i < uNumCourses; i++)
uCourses[i] = readCourse(in, uInstructors, gStudents, uStudents);
//Create University object
university = new University(uName, uTerm, uYear, uStudents, uInstructors, uCourses);
return university;
} catch (FileNotFoundException fe) {
fe.printStackTrace();
return null;
}
}
//Empty File Exception
public class EmptyFileException extends Exception {
String filename;
EmptyFileException() {
}
EmptyFileException(String filename) {
this.filename = filename;
}
public String toString() {
return "The file " + filename + " is empty.";
//Driver
Explanation / Answer
It was telling you that your not catching the exception. So if you run the program, and University.txt is not there, there will be a filenotfound exception. there for you need to catch it in the main, which is the only place the University ufl=University.readFromFile("university.txt"); is located This is also specified in the declaration of class University.... it says throws EmptyFileException. public static void main(String[] args) { try{ University ufl = University.readFromFile("university.txt"); if (ufl != null) { System.out.println(" --Printing out University info--"); System.out.println(ufl); System.out.println(" --Printing out Students info--"); Student[] students = ufl.getStudents(); for (Student s : students) System.out.println(" " + s); } }} catch (Exception e) { System.out.println("Error, file not found"); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.