I'm not sure if this is intentional because I don't know what your data file actually looks like, but the way your while
loop is set up, your only processing every second file line. The while
loop condition right away reads a file line upon each iteration
while (reader.readLine() != null) {
then another line is read in to actually get split:
studentInfo = reader.readLine().split(",");
On each iteration, it will actually be the second line that gets processed and added to the LinkedList. Like I said however, I'm not sure if you are doing this intentionally. If not a usual way would be something like this:
String line;
while ((line = reader.readLine()) != null) {
line = line.trim(); // Trim Leading/Trailing spacing
// Skip Blank Lines:
if (line.isEmpty()) {
continue;
}
// Split the line. The regex handles any delimiter spacing situations (if any):
studentInfo = line.split("\\s*,\\s*");
// ... The rest of loop code ...
}
All in all, you could have something like this (read comments in code):
// Class member variable:
LinkedList<Student> students = new LinkedList<>();
private void readStudentsFromFile(String fileName) throws IOException {
/* 'Try With resouces' used here to auto-close file and free resources.
This will read ANSI or UTF-8 created files. */
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(fileName), "UTF-8"))) {
String[] studentInfo;
String line;
while ((line = reader.readLine()) != null) {
line = line.trim(); // Trim Leading/Trailing spacing
// Skip Blank Lines:
if (line.isEmpty()) {
continue;
}
// Split the line. The regex handles any delimiter spacing situations (if any):
studentInfo = line.split("\\s*,\\s*");
/* Verify Data - Provide alternate if not available or incorrect
(using Ternary Operator). Change or remove as you see fit. */
// Must have at least two characters in Name otherwise "UNKNOWN" is provided:
String name = studentInfo[0].matches(".{2,}") ? studentInfo[0] : "UNKNOWN";
// Must have at least two characters in Country name otherwise "UNKNOWN" is provided:
String address = studentInfo[1].matches(".{2,}") ? studentInfo[1] : "UNKNOWN";
// Must be a Integer number (only digits) otherwise -1 is provided:
int id = studentInfo[2].matches("\\d+") ? Integer.parseInt(studentInfo[2]) : -1;
// Must be a Integer number (only digits) otherwise -1 is provided:
int grade = studentInfo[3].matches("\\d+") ? Integer.parseInt(studentInfo[3]) : -1;
// Must be available and Must be a Integer number (only digits) otherwise -1 is provided.
int age = studentInfo.length == 5 && studentInfo[4].matches("\\d+") ? Integer.parseInt(studentInfo[4]) : -1;
// Add student to students linked list.
students.add(new Student(name, address, id, grade, age));
}
}
}
To use the above method might be something like this:
String fileName = "/Users/noor/NetBeansProjects/student_file.txt";
try {
readStudentsFromFile(fileName);
}
catch (IOException ex) {
// Handle the exception (if any) the way you want).
System.out.println("readStudentsFromFile() Method Error!\n" + ex);
}
// Display List of Students:
for (Student std : students) {
// The Student class was supplied A toString() method:
System.out.println(std.toString());
}
The Student class might look something like this:
public class Student {
private String name;
private String address;
private int id;
private int grade;
private int age;
public Student(String name, String address, int id, int grade, int age) {
this.name = name;
this.address = address;
this.id = id;
this.grade = grade;
this.age = age;
}
@Override
public String toString() {
return name + "\t" + address + "\t" + id + "\t" + grade + "\t" + age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}