'How to delete a line from text file when the submit button is clicked?

This is my code for the submit button

// i'm fetching the information from appointment.txt

// once submit button is clicked, I want the data to go in completed.txt

// and once completed, the line should remove from appointment.txt

private void jSubmitActionPerformed(java.awt.event.ActionEvent evt) {
                                        
        String address = jAddress.getText();
        String paid = jPaid.getText();
        String feedback = jFeedback.getText();
        int amount = Integer.parseInt(jAmount.getText());
        String id = jId.getText();


        if (id.isEmpty() || address.isEmpty() || paid.isEmpty() || id.isEmpty() || feedback.isEmpty() ) {

            JOptionPane.showMessageDialog(this, "Fill in all fields");

        } else{
            PaymentFeedback pf = new PaymentFeedback();
            pf.setId(id);
            pf.setAddress(address);
            pf.setPaid(paid);
            pf.setAmount(amount);
            pf.setFeedback(feedback);


            if(pf.verifyPayment()== true){

                JOptionPane.showMessageDialog(null, "Record Exist", " PAYMENT RECEIVED", JOptionPane.ERROR_MESSAGE);
            }
            else{
                pf.addPayment();
                jId.setText(null);
                jAddress.setText(null);
                jAmount.setText(null);
                jFeedback.setText(null);
            }
        }
        ArrayList<String> tempArray = new ArrayList<>();
          
        try(FileReader fr = new FileReader("completed.txt")){
            Scanner scanner = new Scanner(fr);
            String line;
            String [] lineArray;
              
                while((line = scanner.nextLine())!= null){
                    lineArray = line.split("\\,");
                    if(lineArray[0].equals(id)){
                        tempArray.remove(lineArray);
                        
                }
                    else{
                        tempArray.add(line);
                    }
                }
            fr.close();
        }
        catch(Exception e){
            
        }
        
        ArrayList<String> delArray = new ArrayList<>();
          
        try(FileReader fr = new FileReader("appointment.txt")){
            Scanner scanner = new Scanner(fr);
            String line;
            String [] lineArray;
              
            while((line = scanner.nextLine())!= null){
                lineArray = line.split("\\,");
                if(lineArray[0].equals(id)){
                    delArray.remove(lineArray);
                    JOptionPane.showMessageDialog(null, "Booking Deleted");
                }
                else{
                    delArray.add(line);
                }
            }
            fr.close();
        }
        catch(Exception e){
              
        }
        try(PrintWriter pw = new PrintWriter("appointment.txt")){
            for(String s : tempArray){
                pw.println(s);
            }
            pw.close();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(this, "ERROR","Failed to Delete Booking", JOptionPane.ERROR_MESSAGE);
        }       
  }

// this is my java class for completed.txt

public class PaymentFeedback {
    
    private String id;
    private String address;
    private String paid;
    private int amount;
    private String feedback;
    
    
    
    public boolean verifyPayment(){    
        boolean valid = false;
        try(Scanner scan = new Scanner(new File("completed.txt"))){
            scan.nextLine(); //CODE to skip first line of file
            while(scan.hasNextLine()){
                String line = scan.nextLine();// break the line into pieces in array
                String[] pieces = line.split("\\,"); //the string is a separator
                if (this.getId().equalsIgnoreCase(pieces[0])) {
                    valid = true;
                    break;
                }
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        return valid;
    }
    
    public boolean addPayment(){
        boolean valid = false;
        try{
            FileWriter fw = new FileWriter ("completed.txt", true);
            try(BufferedWriter bw = new BufferedWriter (fw)){
                bw.append (this.getId());
                bw.append("," + this.getAddress());
                bw.append("," + this.getPaid());
                bw.append("," + this.getAmount());
                bw.append("," + this.getFeedback());
                bw.newLine();
                bw.close();
           
                JOptionPane.showMessageDialog(null, "Saving Successful", "Information", JOptionPane.INFORMATION_MESSAGE);
                valid = true;
            }
        }
        catch(HeadlessException | IOException e){
            Logger.getAnonymousLogger().log(Level.CONFIG,"msg", e);
        }
        return valid;
    }
    
    public String getAddress(){
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPaid(){
        return paid;
    }
    public void setPaid(String paid) {
        this.paid = paid;
    }
    
    public int getAmount(){
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    
    public String getFeedback(){
        return feedback;
    }
    public void setFeedback(String feedback) {
        this.feedback = feedback;
    }
    public String getId(){
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
}

// this is my java class for appointment.txt

public class Appointments {

    private String id ;
    private String fname;
    private String lname;
    private int number;
    private String time;
    private String date;
    private String address;
    private String technician;
    private String description;


public boolean verifyAppointment(){ 
    boolean valid = false;
    try(Scanner scan = new Scanner(new File("appointment.txt"))){
        scan.nextLine(); //CODE to skip first line of file
        while(scan.hasNextLine()){
            String line = scan.nextLine();// break the line into pieces in array
            String[] pieces = line.split("\\,"); //the string is a separator
            if (this.getFname().equalsIgnoreCase(pieces[1])) {
                valid = true;
                break;
            }
        }
    }catch(Exception e){
        System.out.println(e.getMessage());
        }
        return valid;
    }

public boolean addAppointment(){
        boolean valid = false;
        try{
            FileWriter fw = new FileWriter ("appointment.txt", true);
            try(BufferedWriter bw = new BufferedWriter (fw)){
                bw.append ( this.getId());
                bw.append("," + this.getFname());
                bw.append("," + this.getLname());
                bw.append("," + this.getNumber());
                bw.append("," + this.getTime());
                bw.append("," + this.getDate());
                bw.append("," + this.getAddress());
                bw.append("," + this.getTechnician());
                bw.append("," + this.getDescription());
                bw.newLine();
                bw.close();
           
                JOptionPane.showMessageDialog(null, "Saving Successful", "Information", JOptionPane.INFORMATION_MESSAGE);
                valid = true;
            }
        }
        catch(HeadlessException | IOException e){
            Logger.getAnonymousLogger().log(Level.CONFIG,"msg", e);
        }
        return valid;
    }
    

    public String getId(){
        return id;
    }
    public void setId(String id){
        this.id = id;
    }
    
    public String getFname(){
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getLname(){
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
    
    public int getNumber(){
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    
    public String getTime(){
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    } 
    
    public String getDate(){
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    
    public String getAddress(){
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getTechnician(){
        return technician;
    }
    public void setTechnician(String technician) {
        this.technician = technician;
    }
    public String getDescription(){
        return description;
    }
    public void setDescription(String description) {
        this. description = description;
    }

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source