1

I'm currently developing a basic function that deletes specific customer entries from a txt file, currently the code I do have only searches for the customer's name and deletes only the name. How do I change this so it deletes all of the customers information as opposed to just their name from the txt file?

Customer data within the txt file is formatted in this manner:

Dave Ted
10 Roberts Drive
12345
1000
B&Q £23.72, Úber £13.50, Eleanor Humphries £78.21
Mike Valencia
9 Farrels Way
56789
100
Cineworld £20.00, TGI £39.30, RadioShack £60.19

method from the code

public static void removeCus()throws IOException{

    Scanner stdin = new Scanner( System.in);
    String name,address,id,balance,transactions,record;;

    File inputFile = new File("text_files/customers.txt"); 
    File tempFile = new File("text_files/tempFile.txt"); 

    BufferedReader br = new BufferedReader(new FileReader(inputFile)); 
    BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile)); 

    System.out.println("Enter the Customers name: ");
    name =  stdin.nextLine();

    while( ( record = br.readLine() ) != null ) {
        if( record.contains(name) ) 
            continue;

        bw.write(record);
        bw.flush();
        bw.newLine();
    }

    br.close();
    bw.close();
    inputFile.delete();
    tempFile.renameTo(inputFile); 

    boolean successful = tempFile.renameTo(inputFile); 
}
cigien
  • 57,834
  • 11
  • 73
  • 112
Knave
  • 29
  • 4

1 Answers1

2

You need to ignore the next 4 lines for this. Something like this:

System.out.println("Enter the Customers name: ");
name =  stdin.nextLine();
int ignoreLines = 0; // set this value to 4 when customer name is found

while((record = br.readLine()) != null) {
    if(record.contains(name)) {
        ignoreLines = 4;  // ignore next 4 lines
        continue;
    }

    if(ignoreLines > 0) {
        ignoreLines--;
        continue;
    }

    bw.write(record);
    bw.flush();
    bw.newLine();
}
Mushif Ali Nawaz
  • 3,707
  • 3
  • 18
  • 31