hii ^^
I have a problem which is I have to delete a book when( I enter it's name and then press the delete button) and that book has been saved as an object in an array of list, and then written to a (txt )file.. once I press the delete button and then open that Txt file (I still find that book ) but in fact it has been deleted ! I noticed that when I save a new book after that and then open the txt file ,what I found that this file has only the new book which I recently saved and the deleted book has gone !
so how can I solve the prob. >> that when I press the delete button ,that book should be deleted at that time from the file !so when I open the file that book should be deleted !
NOTE : the update button has the same problem :((
here is the code of the buttons !
thanks ^
^

 private void SaveBActionPerformed(java.awt.event.ActionEvent evt) {                                      

       book object ;
       createFile ();
        save ();  
        for (int i = 0;i<saveBook.size();i++){
        object=(book)saveBook.get(i);
        writer.format ("%s %s %d %s %.2f\r\n",object.getName(),object.getAuthor(),object.getSerialn(),object.getDate(),object.getPrice());
        }
        Tf1.setText("");
        Tf2.setText("");
        Tf3.setText("");
        Tf4.setText("");
        Tf5.setText("");

        closeFile ();



    }                                     

    private void DeleteBActionPerformed(java.awt.event.ActionEvent evt) {                                        

 book DeleteBook=new book();
        readFile ();
        String nameVariable = Tf1.getText();
      //  System.out.println (nameVariable);
         for (int i = 0;i<saveBook.size();i++){
        DeleteBook=(book)saveBook.get(i);
       String nameVariable2= DeleteBook.getName();

       if (nameVariable.compareTo(nameVariable2)==0){
  saveBook.remove(i);

       }
    }                                       
    Tf1.setText("");
        Tf2.setText("");
        Tf3.setText("");
        Tf4.setText("");
        Tf5.setText("");
   }
    private void UpdateBActionPerformed(java.awt.event.ActionEvent evt) {                                        

      book UpdateBook=new book();
        readFile ();
        String nameVariable = Tf1.getText();
      //  System.out.println (nameVariable);
         for (int i = 0;i<saveBook.size();i++){
        UpdateBook=(book)saveBook.get(i);
       String nameVariable2= UpdateBook.getName();

       if (nameVariable.compareTo(nameVariable2)==0){
  int variable =i;
 String Bookname;
 String AuthorBook;
 int SerialNum;
String datePublish;
 double PriceB;
 //Declartion of variables
 Bookname=Tf1.getText();
AuthorBook=Tf2.getText();
SerialNum=Integer.parseInt(Tf3.getText());
datePublish=Tf4.getText();
PriceB=Double.parseDouble(Tf5.getText());
 UpdateBook.setName(Bookname);
  UpdateBook.setAuthor(AuthorBook);
 UpdateBook.setSerialn(SerialNum);
 UpdateBook.setDate(datePublish);
 UpdateBook.setPrice(PriceB);
  saveBook.add(UpdateBook);
  saveBook.remove(i);
 //writer.format ("%s %s %d %s %.2f\r\n",UpdateBook.getName(),UpdateBook.getAuthor(),UpdateBook.getSerialn(),UpdateBook.getDate(),UpdateBook.getPrice());

       }
        }

       // closeFileRead ();
    //    closeFile ();









    }                                       

    private void SearchBActionPerformed(java.awt.event.ActionEvent evt) {                                        
    book pBook ;
        readFile ();
        String nameVariable = Tf1.getText();
      //  System.out.println (nameVariable);
         for (int i = 0;i<saveBook.size();i++){
        pBook=(book)saveBook.get(i);
       String nameVariable2= pBook.getName();
         //      System.out.println (nameVariable2);

       if (nameVariable.compareTo(nameVariable2)==0){



       Tf1.setText(pBook.getName());
        Tf2.setText(pBook.getAuthor());
        String SNString = String.valueOf(pBook.getSerialn());
        Tf3.setText(SNString);
        Tf4.setText(pBook.getDate());
        String priceString = String.valueOf(pBook.getPrice());

        Tf5.setText(priceString);

       }
        }

        closeFileRead ();



    }                                       

    //function Create
    public void createFile (){
 try {
 writer=new Formatter ("Book.txt");
 }
 catch (Exception ex){

 System.err.println ("Error");
 }

 }
    //Function SAve
   public void save (){
 book objB=new book ();
String Bookname;
 String AuthorBook;
 int SerialNum;
String datePublish;
 double PriceB;
 //Declartion of variables
 Bookname=Tf1.getText();
AuthorBook=Tf2.getText();
SerialNum=Integer.parseInt(Tf3.getText());
datePublish=Tf4.getText();
PriceB=Double.parseDouble(Tf5.getText());
 objB.setName(Bookname);
  objB.setAuthor(AuthorBook);
 objB.setSerialn(SerialNum);
 objB.setDate(datePublish);
 objB.setPrice(PriceB);
 saveBook.add(objB);

    }
public void readFile(){

 try {
 reader=new Scanner (new File("Book.txt"));

 }
 catch (Exception ex){

 System.err.printf("Error");

 }

 }
public void  closeFileRead (){
if (reader !=null) {

reader.close ();

}

}

 public void closeFile (){
 if (writer !=null ){
 writer.close ();
 }

}

the code of the book class which is in the same pachage :P

import java.util.Date;
public class book {
  private String BookN;
private String Author;
private int SerialN;
private String date;
private double Price;
public book (){
this ("","",0,"",0.0);
}
 public book (String BN,String A,int SN,String d,double P){
   setName (BN);
   setAuthor (A);
   setSerialn(SN);
   setDate(d);
   setPrice(P);
    }
 public void setName (String n){
 BookN=n;
 }
  public void setAuthor (String A){Author=A;}
  public void setSerialn(int no){SerialN=no;}
   public void setDate(String d){date=d;}
   public void setPrice(double p){Price=p;}

 public String  getName (){
 return BookN;
 }
 public String getAuthor (){return Author;}
  public int getSerialn(){return SerialN;}
   public String getDate(){return date;}
   public Double getPrice(){return Price;}
}

--------------------------
the class of the framedesign !

public class Hw1 extends javax.swing.JFrame {

 private ArrayList saveBook;
  private Formatter writer;
    private Formatter writer2;
 private File f;
private Scanner reader;

    public Hw1() {
        initComponents();
               saveBook=new ArrayList ();

    }

Edited 1 Year Ago by shahera.arafat

Some comments:

That code is hard to read because:
The indentation is all over the place - use NetBeans to forma the code properly
You ignore Java naming conventions - class names should always start with a Capital letter, variables with a lower case letter

Declare the list as ArrayList<Book> saveBook= new ArrayList<>(); so Java knpws it only contains Books and thus you don't need to cast the elements when you get them

The file reading strategy is confused.
You only need to read the file into saveBook once, when the program starts. Then apply all your add/delete/updates to saveBook, and write the updated version to the file.

mm..look what I did .. I think the problem was because of I forgot to put the ( writer.format ("","","","","");)
which writes on the file >>
but once I do that it gives me an exception >>(FormattedClosedException)
so do U have any idea what is the cuase of that ??

here is the updated code !

 private void DeleteBActionPerformed(java.awt.event.ActionEvent evt) {                                        

        book DeleteBook;
       // readFile ();
        String nameVariable = Tf1.getText();
      //  System.out.println (nameVariable);
         for (int i = 0;i<saveBook.size();i++){
        DeleteBook=(book)saveBook.get(i);
       String nameVariable2= DeleteBook.getName();

       if (nameVariable.compareTo(nameVariable2)==0){
       saveBook.remove(i);

       }
        writer.format ("","","","","");

    }                                       
        Tf1.setText("");
        Tf2.setText("");
        Tf3.setText("");
        Tf4.setText("");
        Tf5.setText("");
   }
This question has already been answered. Start a new discussion instead.