Help with File Processing
my code is compiling but it won't run. can someone help me with this? i've been having trouble for days. my instructor said that
Code Java:
Student[] studs = new Student[Integer.parseInt(fr.nextLine())];
the nextLine() is not compatible with the parseInt but i have no idea what to change on that line. any suggestions?
Code Java:
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
fr = new Scanner (new File("students.csv"));
// 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();
System.out.println(s2);
} catch (Exception i) {
i.printStackTrace();
}
// close input file...
ois.close();
// verify read data...
//ystem.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();
*/
}
}
Re: Help with File Processing
What does the first line of the data file look like?
Re: Help with File Processing
like this
Quote:
2123259,AVILA,JEREMY RAYMUND,T,BSIT,1
Re: Help with File Processing
So this part:
Code :
Integer.parseInt(fr.nextLine())
of the statement:
Code :
Student[] studs = new Student[Integer.parseInt(fr.nextLine())];
is attempting to convert the whole first line of the file, which is this:
Code :
2123259,AVILA,JEREMY RAYMUND,T,BSIT,1
to an integer, and it's complaining (as it should).
Review your code comment:
Code :
// Read the first line of the text file as basis for the number
// of elements for the array of Students to be declared
Is the first line of the data file that you posted going to tell you how many Students to create? No. Is that first line missing, or is the file right and the code comment wrong?
Re: Help with File Processing
Okay, so I've changed the line
Code java:
Student[] studs = new Student[Integer.parseInt(fr.nextLine())];
to a more straight forward revision just to see what happens like ... = new Student[107]; and now I'm getting this kind of results
Quote:
Student ID: 2115232
Student name: VILLANUEVA, SHEENA M.
CourseBSIT-0
null
null
null
null
null
java.lang.ClassCastException: [LStudent; cannot be cast to Student
at SampleFileProcessing.printToFile(SampleFileProcess ing.java:72)
at SampleFileProcessing.run(SampleFileProcessing.java :47)
at SampleFileProcessing.main(SampleFileProcessing.jav a:10)
but I don't know why..
Re: Help with File Processing
The error refers to a cast of LStudent to a Student, as in:
LStudent lStudent = new LStudent();
Student student = (Student)lStudent;
What's LStudent? Did you rename the Student class as part of your versioning scheme and forget to change a statement like this one:
Student s2 = (Student) ois.readObject();