hi guys, i'm really having a trouble with my conversion code... it says the NumberFormatException with this "Student[] studs = new Student[Integer.parseInt(fr.nextLine())];" but i have no idea why. can someone help me?
import java.util.Scanner;
import java.io.*;
public class SampleFileProcessing implements Serializable{
private static Scanner fr;
public static void main(String[] args) throws IOException {
SampleFileProcessing tfr = new SampleFileProcessing();
tfr.run();
}
private static void run() throws IOException {
// To read the input file
try {
fr = new Scanner (new File("students.csv"));
} catch (Exception e) {
e.printStackTrace();
}
// Read the first line of the text file as basis for the number
// of elements for the array of Students to be declared
Student[] studs = new Student[Integer.parseInt(fr.nextLine())];
int index = 0;
// Read each of the records contained in the text file
// and store them into the elements of the array
while (fr.hasNextLine()){
String[] temp = fr.nextLine().split(",");
// Store the values to the following variables
String id = temp[0];
String lastName = temp[1];
String firstName = temp[2];
String middleInit = temp[3];
String course = temp[4];
int year = Integer.parseInt(temp[5]);
// Use the values in instantiating Student object
studs[index] = new Student(id, lastName, firstName, middleInit, course, year);
// Alternatively, the previous lines can be shortened
// using the statement below:
// studs[index] = new Student(temp[0], temp[1], Integer.parseInt(temp[2]),
// Integer.parseInt(temp[3]));
}
// output
printToScreen(studs);
printToFile(studs);
}
private static void printToScreen(Student[] st) {
// To output the array objects (screen) using the enhanced for statement
for (Student s: st) {
System.out.println(s); // toString() method is called automatically
}
}
private static void printToFile(Student[] s) throws IOException {
// To output records in an output file: output.txt
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.dat"));
//write object to output file
oos.writeObject(s);
//close file
oos.close();
// open created file as input file...
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.dat"));
// read object data from file into object variable (note the type cast)...
try{
Student s2 = (Student) ois.readObject();
} catch (Exception i) {
i.printStackTrace();
}
// close input file...
ois.close();
// verify read data...
System.out.println(s2);
//PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"));
/*for (int i = 0; i < s.length; i++){
outFile.println("ID: " + s[i].getID());
outFile.println("Name: " + s[i].getLastName() + ", " + s[i].getFirstName() + " " + s[i].getMiddleInit() + ".");;
outFile.println("Course and Year: " + s[i].getCourse());
outFile.println("================================");
}
outFile.close();
*/
}
}