java - How to increment a number from a saved text file? -


*how increment number read saved text file?

i'm trying add account number read file array , when add new data array account number should auto increment of existing account number. problem code woks fine until save file. when re-open application account number starts beginning i.e if saved acc number 100,101 when re-open application acc number starts 100 again. please can 1 me read file , write file.*

the array starting beginning when restart application.but wondering if 1 can me find solution this

this code

import java.io.serializable;  public class student implements serializable {     //--------------------------------------------------------------------------         protected static int nextbankid=1000;     protected int accountnumber;         protected string fname;     protected string lname;     protected string phone;      //--------------------------------------------------------------------------        //constructor     public student(string fname, string lname, string phone){              this.accountnumber = nextbankid++;             this.fname = fname;             this.lname = lname;             this.phone=phone;                      }     //--------------------------------------------------------------------------     public void setfname(string fname) {         this.fname = fname;     }     //--------------------------------------------------------------------------     public void setlname(string lname) {         this.lname = lname;     }     //--------------------------------------------------------------------------     public void setphone(string phone) {         this.phone = phone;     }     //--------------------------------------------------------------------------     public int getaccountnumber() {         return accountnumber;     }     //--------------------------------------------------------------------------     public void setaccountnumber(int accountnumber) {         this.accountnumber = accountnumber;     }      //--------------------------------------------------------------------------     public string getfname() {         return fname;     }     //--------------------------------------------------------------------------     public string getlname() {         return lname;     }     //--------------------------------------------------------------------------     public string getphone() {         return phone;     }        @override     public string tostring(){          return "\n #acc\tfirst name"+"\tlast name\t#phone"         + "\tbalance"+ "\toverdraft\t\t\n"+accountnumber+""                 + "\t"+fname+"\t"+lname+"\t"+phone;      }  }   //-------------------------------------------------------  import java.io.*; import java.util.arraylist; import javax.swing.joptionpane;  @suppresswarnings("unchecked")  public class readwrite{      //save stuaccount arraylist stuaccount objects file public static void writeaccounts(string filename,arraylist<student> stu) {         //create stream between program , file         try         {             fileoutputstream fostream = new fileoutputstream(filename);             objectoutputstream outstream = new objectoutputstream(fostream);              outstream.writeobject(stu);             outstream.close();         }         catch(filenotfoundexception fnfex)         {               joptionpane.showmessagedialog(null,fnfex.getmessage());         }         catch(ioexception ioe)         {                joptionpane.showmessagedialog(null,ioe.getmessage());         }    }         //read stuaccount arraylist  public static arraylist<student> readaccounts(string filename,arraylist<student> stu){ //throws ioexception       try     {     //joptionpane.showmessagedialog(null, "enter subject details display");             //studentdriver.createsubject();  //create subject store stuaccount objects                  fileinputstream fistream = new fileinputstream(filename);                 objectinputstream instream = new objectinputstream(fistream);                  stu = (arraylist<student>)instream.readobject();                 //© increment's stu account 1                 (int i=0; i< stu.size(); i++){                      object b1 = stu.get(i);                 if (b1 instanceof student){                                 student baccount = (student)b1;                 if(baccount.accountnumber==stu.get(i).getaccountnumber())                     baccount.accountnumber=stu.get(i).getaccountnumber();                     }                  }//for next user entry                 instream.close();          }         catch(classnotfoundexception cnfex){             joptionpane.showmessagedialog(null,cnfex.getmessage());         }                catch(filenotfoundexception fnfex){             joptionpane.showmessagedialog(null,fnfex.getmessage());         }         catch(ioexception ioe){             joptionpane.showmessagedialog(null,ioe.getmessage());         }                  joptionpane.showmessagedialog(null, ""+stu.size()                         + " stu account(s) found in database....");      return stu; //return read objects driver class     } }   // ----------------------------------------------   import java.awt.font; import java.io.ioexception; import java.util.arraylist; import javax.swing.joptionpane; import javax.swing.jtextarea;  public class studentdriver {  public static arraylist<student> stu = new arraylist<>(); private static string filename = "student.txt";  //------------------------------------------------------------------------------ public static void main(string [] vimal)  throws ioexception{     try{             stu = readwrite.readaccounts(filename,stu);         if(stu.isempty()){                 joptionpane.showmessagedialog(null,"no account's found "                 + "in datbase load memory!\n\n");             }else{             joptionpane.showmessagedialog(null,"file contents "                     + "read , loaded memory!\n");        }     }     catch (exception e) {                         joptionpane.showmessagedialog(null,"error encountered "                                 + "while opening file"                                 + "\ncould not open file"                                 + "\n" + e.tostring());           }     //initialise main menu choice;               //------------------------------------------------------------------------------                int choice= mainmenu();        while (choice!=3){       switch (choice){             case 1:                 createstudentaccount();//a new account menu                 break;             case 2:                 list();//list method                             break;                                case -100:                               joptionpane.showmessagedialog(null,"you have selected cancel");                     break;                       case -99:                                joptionpane.showmessagedialog(null,"you must select  1-3");                     break;                       default:                                 joptionpane.showmessagedialog(null,"not valid option .plese"                                                         + " re-enter option");                     break;                }//end fo switch statement        choice = mainmenu();     }//end of while loop    readwrite.writeaccounts(filename,stu); //save arraylist contents before exiting     system.exit(0);                    }      //end main , program exits here    public static int mainmenu(){      string menu="us college\n main menu\n1.add new student\n2. print all\n3. exit\n\n";      string userselection = (string)joptionpane.showinputdialog(null,menu);          int option = validateselection(userselection);     return  option; }  //------------------------------------------------------------------------------ //                 create account menu selection validation  public static int validateselection(string createaccount){     //enter cancel     if (createaccount==null)             return-100;       //hit enter without entry = zero-length string     if (createaccount.length()<1)                    return-99;      //entered more 1 charecter        if (createaccount.length()>1)                    return-101;      if (createaccount.charat(0)< 49  ||                                         createaccount.charat(0)>51)                   return-101;     else                 return integer.parseint(createaccount); }    public static void createstudentaccount(){          string acctfirstname,acctlastname,strphone;         try{                    //account name validation          acctfirstname = joptionpane.showinputdialog(null,"enter first name");          acctlastname = joptionpane.showinputdialog(null,"enter family name");          strphone =joptionpane.showinputdialog(null,"enter phone nuber");               stu.add(new student(acctfirstname,                                         acctlastname,strphone));      }  catch(exception e){}                 }   public static void list(){           string accounts = "";          string college="\t======== college ========\n";          jtextarea output = new jtextarea();          output.setfont(new font("ariel", font.plain, 12));    if(stu.isempty()){             joptionpane.showmessagedialog(null,"no account's created yet");          }    else{                //for each bank account in  bankaccount arraylist          (student s1: stu){                   if(stu.isempty()){                 joptionpane.showmessagedialog(null,"no account's created yet");                         }else{                            accounts += s1.tostring();                         }           output.settext(college+accounts );                  }          joptionpane.showmessagedialog(null,output);     }   } } 

first create table maintaing account number.after writing text file, every time use update table of previous account no new account no incrementing value of account no .


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - EclipseLink JPA Object is not a known entity type -

java - Need to add SOAP security token -