public void importStudent(String fileName) throws FileNotFoundException{
File input = new File("students.txt");
Scanner readFile = new Scanner(input);
ArrayList<Object> tokensList = new ArrayList<Object>();
while (readFile.hasNextLine()){
tokensList.add(readFile.nextLine().split(","));
String FirstName = (String) tokensList.get(0);
String LastName = (String) tokensList.get(1);
String phoneNum = (String) tokensList.get(2);
String address = (String) tokensList.get(3);
double gpa = (double) tokensList.get(4);
String major = (String) tokensList.get(5);
double creditsTaking = (double) tokensList.get(6);
//all of the stuff in one line of the text file
Student s = new Student( FirstName, LastName, phoneNum, address, gpa,
major, creditsTaking);
peopleBag.add(s);
}
readFile.close();
}
So i have a text file in which each line has all of the information for one object of Student class that I am trying to create. What I want to do is read one line of the text file, add of the info to an array list, then use that list to fulfill all of the fields of my Student constructor. This method has no red lines, but I get the following error when I run this method:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String at step1.PeopleBag.importStudent(PeopleBag.java:35) at step1.Demo.main(Demo.java:10)
tokensListArrayList altogether.