I'm new to java and I have a problem with deleting specific data based on the student id from user input. I can only delete the student id but not all the info about that student.
Here is my code:
File inputFile = new File("test.txt");
File tempfile = new File("temp.txt");
Scanner scanner = new Scanner("test.txt");
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
PrintWriter wr = new PrintWriter("temp.txt");
String s = null;
String infoToRemove = JOptionPane.showInputDialog(null, "Enter ID to cancel");
while ((s = br.readLine()) != null) {
for (int i = 0; i < s.length(); i++) {
if (s.indexOf(infoToRemove) != -1) {
s = s.replaceAll(infoToRemove, "");
}
}
wr.println(s);
}
wr.close();
br.close();
inputFile.delete();
tempfile.renameTo(inputFile);
Expected Output:
Before:
jason,100,20,Male,Block 1,Ground Floor
jack,200,20,Male,Block 1,Ground Floor
After user insert ID=100
to delete:
jack,200,20,Male,Block 1,Ground Floor