Helpful Information
 
 
Category: Java
reading a file

I have a program in which i need to read a file. I am using a try and catch statement, but everythign i try and bring in the file it catches somethign and does not aquire the information.

here is the code
import java.util.*;
import java.io.*;
class Project10Driver
{
static int age, id, graduationYear, credLeft, apCred, SAT, examOne, examTwo, finalExam, gre;
static double gpa;
static String schoolName, name, degree;
static Scanner scan = new Scanner(System.in);
static int userChoice = 1;
static int counter = 0;
static Student[] ListOfStudents = new Student[50];
final static int LIMIT = 14;
static int ap, pTime;
static String major, minor;
static boolean time;
public static void main(String[]args) throws IOException
{
// the following 2 lines make it work with spaces in strings.
//String blah = System.getProperty("line.separator");
//scan.useDelimiter(blah);

while (userChoice != 0)
{
menu();
userChoice = scan.nextInt();
menuSwitch(userChoice);
}

}
public static void getInputFromFile()
{
String line = "";

System.out.print("Please enter the name of the file: ");
String fileName = scan.next();
try{
Scanner scan3 = new Scanner (new File(fileName));
while(scan3.hasNext()) {
line = scan3.nextLine();


if (line.equals("MIDDLE"))
{
name = scan3.nextLine();
schoolName = scan3.nextLine();
age = Integer.parseInt(scan3.nextLine());
id = Integer.parseInt(scan3.nextLine());
gpa = Integer.parseInt(scan3.nextLine());
graduationYear = Integer.parseInt(scan3.nextLine());
ListOfStudents[counter] = new MiddleSchool(name, age, schoolName, id, gpa, graduationYear);
counter++;
}
else if (line.equals("HIGH"))
{
name = scan3.nextLine();
schoolName = scan3.nextLine();
age = Integer.parseInt(scan3.nextLine());
id = Integer.parseInt(scan3.nextLine());
gpa = Integer.parseInt(scan3.nextLine());
graduationYear = Integer.parseInt(scan3.nextLine());
credLeft = Integer.parseInt(scan3.nextLine());
SAT = Integer.parseInt(scan3.nextLine());
apCred = Integer.parseInt(scan3.nextLine());

ListOfStudents[counter] = new HighSchool(name, age, schoolName, id, gpa, graduationYear, credLeft, SAT, apCred);
counter++;
}
else if (line.equals("UNDERGRAD"))
{
name = scan3.nextLine();
schoolName = scan3.nextLine();
age = Integer.parseInt(scan3.nextLine());
id = Integer.parseInt(scan3.nextLine());
gpa = Integer.parseInt(scan3.nextLine());
apCred = Integer.parseInt(scan3.nextLine());
major = scan3.nextLine();
minor = scan3.nextLine();
time = false;
examOne = Integer.parseInt(scan3.nextLine());
examTwo = Integer.parseInt(scan3.nextLine());
finalExam = Integer.parseInt(scan3.nextLine());

double[] activity = new double[LIMIT];
double[] exercise = new double[LIMIT];
double[] project = new double[LIMIT];
Scanner scan2 = new Scanner(scan3.nextLine());
for(int x = 0; x < LIMIT; x++)
activity[x] = Double.parseDouble(scan2.nextLine());
scan2 = new Scanner(scan3.nextLine());


for (int i = 0; i < LIMIT; i++)
exercise[i] = Double.parseDouble(scan2.next());
scan2 = new Scanner(scan3.nextLine());


for(int j = 0; j < LIMIT; j++)
project[j] = Double.parseDouble(scan2.next());


ListOfStudents[counter] = new Undergrad(name, age, schoolName, id, gpa, apCred, major, minor, time, examOne, examTwo, activity, exercise, project, finalExam);
counter++;
}
else if (line.equals("GRAD"))
{
name = scan3.nextLine();
schoolName = scan3.nextLine();
age = Integer.parseInt(scan3.next());
id = Integer.parseInt(scan3.next());
gpa = Integer.parseInt(scan3.next());
major = scan3.next();
minor = scan3.next();
time = false;
degree = scan3.next();
gre = Integer.parseInt(scan3.next());

ListOfStudents[counter] = new Graduate(name, age, schoolName, id, gpa, major, minor, time, gre, degree);
counter++;
}
else
System.out.println("Bad File");



//add to the array

}


}
catch(Exception e1)
{
System.err.println("File input error");
}


}

public static void menuSwitch(int choice)
{

int num;
switch(choice)
{
case 1:

MiddleSchool ms= new MiddleSchool();
ms.getUserInput();
ListOfStudents[counter] = ms;
counter++;


break;
case 2:

HighSchool hs = new HighSchool();
hs.getUserInput();
ListOfStudents[counter] = hs;
counter++;


break;
case 3:

Undergrad uGrad = new Undergrad();
uGrad.getUserInput();
ListOfStudents[counter] = uGrad;
counter++;

break;
case 4:


Graduate grad = new Graduate();
grad.getUserInput();
ListOfStudents[counter] = grad;
counter++;


break;
case 5:
System.out.print("Please enter index of student: ");
int index = scan.nextInt();
System.out.print("Please enter desired grade: ");
double desiredGrade = scan.nextDouble();
Undergrad ug = (Undergrad)ListOfStudents[index];
double finalGrade = ug.computeFinalExamGrade(desiredGrade);
System.out.println("To get a "+desiredGrade+" you need to score a "+finalGrade+" on the final exam");

break;

case 6:

for(int i = 0; i < ListOfStudents.length; ++i)
if(ListOfStudents[i] != null)
{
System.out.println();
System.out.println("Student #"+(i+1));
System.out.println(ListOfStudents[i]);
}
System.out.println();
break;

case 7:
getInputFromFile();
break;


default:
for(int i = 0; i < ListOfStudents.length; ++i)
if(ListOfStudents[i] != null){
System.out.println();
System.out.println("Student #"+(i+1));
System.out.println(ListOfStudents[i]);
}
System.out.println();
System.out.println("Goodbye");
break;
}
}

public static void menu()
{
System.out.println("0: Quit");
System.out.println("1: Middle School");
System.out.println("2: High School");
System.out.println("3: College - Undergraduate");
System.out.println("4: College - Graduate");
System.out.println("5: Compute final exam grade needed for a desired grade (undergraduate only) ");
System.out.println("6: Print");
System.out.println("7: Retrieve information for a file.");
System.out.print("\nEnter your choice: ");

}

}
and here is the file it should be retrieving

MIDDLE
John Smith
Liberty Middle School
13
123
2.5
2009
HIGH
Pat Summit
Olympia High School
17
456
3.1
2008
6
1100
3
UNDERGRAD
Billy Bob
Bucknell College
20
7779
3.4
Mechanical Engineering
Spanish
false
1250
8
82
89
0
100 100 100 100 100 100 100 0 100 0 100 100 100 0
90 98 75 60 90 100 78 98 35 100 90 66 78 15
56.4 66.7 98.3 88 78.3 99.8 56.7 94.8 27.5 77 64.5 44.5 88.6 78.9
GRAD
Sue Mae
UNC
24
8702
3.7
Biology and Chemistry
none
true
Masters
1300

Any reason it's not reading it right?










privacy (GDPR)