I am very interested in having a method that can remove a line from a text based file id number, but I don't know how to achieve this.
The file students.txtlooks like this:
1111111,John Smith<br/>
7777777,Dave Smith
Here is what I got so far for the code:
Open class. Student:
public class Student {
private int studentId;
private String name;
public Student(int id, String name) {
this.name = name;
studentId = id;
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public void setId(int newId) {
studentId = newId;
}
public int getId() {
return studentId;
}
}
public class EnrollmentController:
public class EnrollmentController {
private Student theStudent;
private BufferedWriter writer;
private BufferedReader reader;
private final File studentFile = new File("students.txt");
private ArrayList students = new ArrayList();
public EnrollmentController() {
readFromStudentFile();
}
public ArrayList getStudents() {
return students;
}
public void addStudent(int id, String name) {
students.add(new Student(id, name));
}
public void printClassList(String courseId) {
}
public void writeToStudentFile(int id, String name) {
try {
writer = new BufferedWriter(new FileWriter(studentFile, true));
writer.write(id + "," + name + "\n");
writer.flush();
writer.close();
} catch (IOException e) {
System.out.println(e);
}
}
public void readFromStudentFile() {
students = new ArrayList();
try {
reader = new BufferedReader(new FileReader(studentFile));
String line = reader.readLine();
while (line != null) {
String[] record = line.split(",");
int id = Integer.parseInt(record[0]);
Student s = new Student(id, record[1]);
students.add(s);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
System.out.println(e);
}
}
public Student findStudent(int id) {
boolean found = false;
Iterator it = students.iterator();
while (it.hasNext() && !found) {
Student s = (Student) it.next();
if (s.getId() == id) {
found = true;
return s;
}
}
return null;
}
{
boolean found = false;
{
{
found = true;
}
}
}
}
Mike
source
share