The problem: I need to delete a reservation. The user gives me the hour of the reservation trough a SimpleInOutDialog. Then I search with a buffered reader if the file contains that hour.
What I need to do , is delete that line with the hour and 2 lines beneath it because there are the data of the reservation. Here’s and example of the txtFile:
10:00
Niel Butaye
1
09:00
Tom Mullue
2
So I look for 10:00 and then 10:00 , Niel Butaye and 1 need to be deleted.
The code that I already have is:
public void setAnnulation() {
//make SimpleInOutDialog
SimpleInOutDialog input = new SimpleInOutDialog("Delete reservation");
reservation= input.readString("Give the hour (hh:mm)");
try{
BufferedReader br = new BufferedReader(new FileReader("L:\\Documents/Informatica/6de jaar/GIP/Reservations.txt"));
HashSet<String> hs = new HashSet<String>();
int i = 0;
while ((line = br.readLine()) != null)
{
i++;
hs.add(br.readLine());
}
if(hs.contains(reservation)){
//klant bestaat
input.showString("The Reservation is being deleted", "");...}
}catch (Exception e){//Catch wanneer er errors zijn
System.err.println("Error: " + e.getMessage());}
}
Where the “…” needs to come the code. Any help ?
Your approach is foul. Try to handle the file sequentially. Remember that you need to write it anyway so try this:
(open file for reading, open new file for writing)
done.