Prevent duplicate record entry in text file Java -


i have assignment write gui , have information written text file when evaluate button pressed read in data text file sum required fields , if total equal or greater 8000 write records new file creates. have working pretty have hit wall when comes preventing duplicate entries. program not allow duplicate entries. can see there duplicates can't seem printwriter not fire. i've tried boolean if duplicate records exist blocks conditional part writing empty file. appreciated.

    package reprecord;   import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*;          public class dataentry extends jframe     {        private jtextfield repidfield;        private jtextfield repfirstfield;    private jtextfield replastfield;    private jtextfield suppliesfield;    private jtextfield booksfield;    private jtextfield paperfield;    private jradiobutton northradiobutton;    private jradiobutton southradiobutton;    private jradiobutton eastradiobutton;    private jradiobutton westradiobutton;    private buttongroup districtgroup;    private jradiobutton phoneradiobutton;    private jradiobutton emailradiobutton;    private jradiobutton visitradiobutton;    private buttongroup contactgroup;    private jbutton enterbutton;    private jbutton resetbutton;    private jbutton evaluatebutton;    private jbutton exitbutton;    private jlabel labelrepid;     private jlabel labelrepfirst;     private jlabel labelreplast;     private jlabel labelsupplies;     private jlabel labelbooks;     private jlabel labelpaper;    private jlabel labeldistrict;    private jlabel labelcontact;    private jpanel p1;    private jpanel p2;    private jpanel p3;    private jpanel p4;    private jpanel p5;    private jpanel p6;    private jpanel p7;    private jpanel p8;    private jpanel p9;    private jpanel p10;    private jpanel p11;    private jtextarea eval;    private jframe evalframe;    private jtextarea star;    private jframe starframe;    string repid = "";    string repfirst = "";    string replast = "";    string supplies = "";    string books = "";    string paper = "";    string radiotext = "";    string radiotext1 = "";    private int r1;    private string r2;    private string r3;    private double r4;    private double r5;    private double r6;    private string r7;    private string r8;    private int id;    private int id1;    string repdata;    string repdata1;    string initdata;    string stardata;    string stardata1;    int j;   public dataentry()    {       super( "account representative" );       setsize(525, 500);       setvisible(true);       setlocationrelativeto(null);       setresizable(false);       setdefaultcloseoperation(windowconstants.exit_on_close);       container c = new container();       add(c);       c.setlayout(new boxlayout(c, boxlayout.y_axis));        // create , add jpanels components       p1 = new jpanel(new flowlayout(flowlayout.right));       c.add(p1);       p2 = new jpanel(new flowlayout(flowlayout.right));       c.add(p2);       p3 = new jpanel(new flowlayout(flowlayout.right));       c.add(p3);       p4 = new jpanel(new flowlayout(flowlayout.right));       c.add(p4);       p5 = new jpanel(new flowlayout(flowlayout.right));       c.add(p5);       p6 = new jpanel(new flowlayout(flowlayout.right));       c.add(p6);       p7 = new jpanel();       c.add(p7);       p8 = new jpanel();       c.add(p8);       p9 = new jpanel();       c.add(p9);       p10 = new jpanel();       c.add(p10);       p11 = new jpanel();       c.add(p11);               evalframe = new jframe("sales rep data");              eval = new jtextarea();              evalframe.setvisible(false);              evalframe.setsize(575, 300);              evalframe.setlocation(1250, 240);              evalframe.add(eval);              eval.setborder(borderfactory.createlineborder(color.black));              eval.seteditable(false);               file f = new file("salesrep.txt");              if(f.exists() && !f.isdirectory())               {              try                  { // start of try eval                      fileinputstream fs1 = new fileinputstream("salesrep.txt");                      datainputstream in1 = new datainputstream(fs1);                      bufferedreader br1 = new bufferedreader(new inputstreamreader(in1));                    //read file line line                  while ((initdata = br1.readline()) != null)                             {                                eval.append(initdata + "\r\n");                                         }//compare while end                                     in1.close();                          br1.close();                  } // end of try eval              catch (exception e)                  {                     system.err.println("error: " + e.getmessage());                  }              }               starframe = new jframe("star sales rep data");              star = new jtextarea();              starframe.setvisible(false);              starframe.setsize(575, 300);              starframe.setlocation(1250, 560);              starframe.add(star);              star.setborder(borderfactory.createlineborder(color.black));              star.seteditable(false);   // create text fields       labelrepid = new jlabel("representative id");       repidfield = new jtextfield( 15 );       p1.add( labelrepid );       p1.add(box.createrigidarea(new dimension(10,0)));       p1.add( repidfield );       repidfield.requestfocusinwindow();        p1.add(box.createrigidarea(new dimension(100,0)));       p1.add(box.createverticalglue());       p1.add(box.createhorizontalglue());       p1.setborder(borderfactory.createemptyborder(10, 0, 0, 0));         labelrepfirst = new jlabel("representative first name");       repfirstfield = new jtextfield( 15 );       p2.add( labelrepfirst );       p2.add(box.createrigidarea(new dimension(10,0)));       p2.add( repfirstfield );        p2.add(box.createrigidarea(new dimension(100,0)));       p2.add(box.createverticalglue());       p2.add(box.createhorizontalglue());        labelreplast = new jlabel("representative last name");       replastfield = new jtextfield( 15 );       p3.add( labelreplast );       p3.add(box.createrigidarea(new dimension(10,0)));       p3.add( replastfield );       p3.add(box.createrigidarea(new dimension(100,0)));       p3.add(box.createverticalglue());       p3.add(box.createhorizontalglue());        labelsupplies = new jlabel("supplies total sales");       suppliesfield = new jtextfield( 15 );       p4.add( labelsupplies );       p4.add(box.createrigidarea(new dimension(10,0)));       p4.add( suppliesfield );       p4.add(box.createrigidarea(new dimension(100,0)));       p4.add(box.createverticalglue());       p4.add(box.createhorizontalglue());        labelbooks = new jlabel("books total sales");       booksfield = new jtextfield( 15 );        p5.add( labelbooks );       p5.add(box.createrigidarea(new dimension(10,0)));       p5.add( booksfield );       p5.add(box.createrigidarea(new dimension(100,0)));       p5.add(box.createverticalglue());       p5.add(box.createhorizontalglue());        labelpaper = new jlabel("paper total sales");       paperfield = new jtextfield( 15 );       p6.add( labelpaper );       p6.add(box.createrigidarea(new dimension(10,0)));       p6.add( paperfield );       p6.add(box.createrigidarea(new dimension(100,0)));       p6.add(box.createverticalglue());       p6.add(box.createhorizontalglue());  // register event textfields textfieldhandler handler = new textfieldhandler();       repidfield.addactionlistener( handler );       repfirstfield.addactionlistener( handler );       replastfield.addactionlistener( handler );       suppliesfield.addactionlistener( handler );       booksfield.addactionlistener( handler );       paperfield.addactionlistener( handler );   // create radio buttons       northradiobutton = new jradiobutton( "north", false );       southradiobutton = new jradiobutton( "south", false );       eastradiobutton = new jradiobutton( "east", false );       westradiobutton = new jradiobutton( "west", false );       p8.setborder(borderfactory.createtitledborder( null, "sales district"));       p8.add( northradiobutton );        p8.add( southradiobutton );        p8.add( eastradiobutton );       p8.add( westradiobutton );       p8.add(box.createverticalglue());       p8.add(box.createhorizontalglue());        districtgroup = new buttongroup();        districtgroup.add( northradiobutton );        districtgroup.add( southradiobutton );        districtgroup.add( eastradiobutton );        districtgroup.add( westradiobutton );         phoneradiobutton = new jradiobutton( "phone", false );       emailradiobutton = new jradiobutton( "email", false );       visitradiobutton = new jradiobutton( "visit", false );       p9.setborder(borderfactory.createemptyborder(5, 0, 0, 0));       p10.setborder(borderfactory.createtitledborder( null, "preferred contact method"));       p10.add( phoneradiobutton );        p10.add( emailradiobutton );        p10.add( visitradiobutton );       p10.add(box.createverticalglue());       p10.add(box.createhorizontalglue());        contactgroup = new buttongroup();        contactgroup.add( phoneradiobutton );        contactgroup.add( emailradiobutton );        contactgroup.add( visitradiobutton );   // register events radiobuttons radiobuttonhandler handler1 = new radiobuttonhandler();       northradiobutton.additemlistener( handler1 );       southradiobutton.additemlistener( handler1 );       eastradiobutton.additemlistener( handler1 );       westradiobutton.additemlistener( handler1 );       phoneradiobutton.additemlistener( handler1 );       emailradiobutton.additemlistener( handler1 );       visitradiobutton.additemlistener( handler1 );  // create buttons       enterbutton = new jbutton( "   enter  " );        resetbutton = new jbutton( "   reset  " );       evaluatebutton = new jbutton( "evaluate");       exitbutton = new jbutton ("   exit   ");       p11.add( enterbutton );       p11.add(box.createrigidarea(new dimension(20,0)));       p11.add( resetbutton );       p11.add(box.createrigidarea(new dimension(20,0)));       p11.add( evaluatebutton );       p11.add(box.createrigidarea(new dimension(20,0)));       p11.add(exitbutton);       p11.add(box.createverticalglue());       p11.add(box.createhorizontalglue());       p11.setborder(borderfactory.createemptyborder(10, 0, 5, 0));  // register events buttons buttonhandler handler2 = new buttonhandler();       enterbutton.addactionlistener( handler2 );       resetbutton.addactionlistener( handler2 );       evaluatebutton.addactionlistener( handler2 );       exitbutton.addactionlistener( handler2); }   private class textfieldhandler implements actionlistener     {       // process textfield events       public void actionperformed( actionevent event )       {            // user pressed enter in jtextfield repidfield          if ( event.getsource() == repidfield )             repid = event.getactioncommand();             getfocusowner().transferfocus();           // user pressed enter in jtextfield repfirstfield          if ( event.getsource() == repfirstfield )             repfirst = event.getactioncommand();             getfocusowner().transferfocus();           // user pressed enter in jtextfield replastfield          if ( event.getsource() == replastfield )             replast = event.getactioncommand();             getfocusowner().transferfocus();           // user pressed enter in jtextfield suppliesfield          if ( event.getsource() == suppliesfield )             supplies = event.getactioncommand();             getfocusowner().transferfocus();           // user pressed enter in jtextfield booksfield          if ( event.getsource() == booksfield )             books = event.getactioncommand();             getfocusowner().transferfocus();           // user pressed enter in jtextfield paperfield          if ( event.getsource() == paperfield )             paper = event.getactioncommand();             getfocusowner().transferfocus();       }     }   private class radiobuttonhandler implements itemlistener     {       public void itemstatechanged( itemevent e  )       {           if (northradiobutton.isselected())          {              radiotext = northradiobutton.gettext();          }           if (southradiobutton.isselected())          {              radiotext = southradiobutton.gettext();          }           if (eastradiobutton.isselected())          {              radiotext = eastradiobutton.gettext();          }           if (westradiobutton.isselected())          {              radiotext = westradiobutton.gettext();          }           if (phoneradiobutton.isselected())          {              radiotext1 = phoneradiobutton.gettext();          }           if (emailradiobutton.isselected())          {              radiotext1 = emailradiobutton.gettext();          }           if (visitradiobutton.isselected())          {              radiotext1 = visitradiobutton.gettext();          }        }    }   private class buttonhandler implements actionlistener     {       public void actionperformed( actionevent event )       {                   if (event.getsource() == enterbutton)                   {                           r1 = integer.parseint(repid);              r2 = repfirst.substring(0, 1).touppercase() + repfirst.substring(1);              r3 = replast.substring(0, 1).touppercase() + replast.substring(1);              r4 = double.parsedouble(supplies);              r5 = double.parsedouble(books);              r6 = double.parsedouble(paper);              r7 = radiotext.touppercase();              r8 = radiotext1;                      repidfield.settext("");              repid = "";              repfirstfield.settext("");              repfirst = "";              replastfield.settext("");              replast = "";              suppliesfield.settext("");              supplies = "";              booksfield.settext("");              books = "";              paperfield.settext("");              paper = "";              contactgroup.clearselection();              districtgroup.clearselection();               try              {                  filewriter fw = new filewriter("salesrep.txt", true);                  printwriter pw = new printwriter(fw);                  pw.printf("%d %s %s %s $%.2f %s $%.2f %s $%.2f %s %s\r\n",                  r1, r2, r3, "supplies", r4, "books", r5, "paper", r6, r7, r8);                  repdata1 = string.format("%d %s %s %s $%.2f %s $%.2f %s $%.2f %s %s\r\n",                  r1, r2, r3, "supplies", r4, "books", r5, "paper", r6, r7, r8);                  eval.append(repdata1);                  evalframe.setvisible(true);                  pw.close();              }              catch(ioexception e)              {                  system.err.println("error: " + e.getmessage());              }              repidfield.requestfocusinwindow();           }           if (event.getsource() == resetbutton)          {              repidfield.settext("");              repid = "";              repfirstfield.settext("");              repfirst = "";              replastfield.settext("");              replast = "";              suppliesfield.settext("");              supplies = "";              booksfield.settext("");              books = "";              paperfield.settext("");              paper = "";              contactgroup.clearselection();              districtgroup.clearselection();              repidfield.requestfocusinwindow();           }            if (event.getsource() == evaluatebutton)          {                    try              {                   fileinputstream fs = new fileinputstream("salesrep.txt");                   datainputstream in = new datainputstream(fs);                   bufferedreader br = new bufferedreader(new inputstreamreader(in));               //read file line line , split @ spaces              while ((repdata = br.readline()) != null)                 {                   string[] rd = repdata.split(" ");                   id = integer.parseint(rd[0]);                   string first = rd[1].substring(0,1).touppercase() + rd[1].substring(1);                   string last = rd[2].substring(0,1).touppercase() + rd[2].substring(1);                   string sup = rd[4];                   sup = sup.substring(1);                   double sup1 = double.parsedouble(sup);                   string bks = rd[6];                   bks = bks.substring(1);                   double bks1 = double.parsedouble(bks);                   string pap = rd[8];                   pap = pap.substring(1);                   double pap1 = double.parsedouble(pap);                   string dis = rd[9].touppercase();                   string con = rd[10];                file f = new file("stars.txt");              if(f.exists() && !f.isdirectory())               {                    try              {                   fileinputstream fs1 = new fileinputstream("stars.txt");                   datainputstream in1 = new datainputstream(fs1);                   bufferedreader br1 = new bufferedreader(new inputstreamreader(in1));               while ((stardata = br1.readline()) != null)                 {                   string[] rd1 = stardata.split(" ");                   id1 = integer.parseint(rd1[0]);                   if(id == id1)                   {                        system.out.printf("\n%s %d %s", "duplicate record item " , id, "\n");                        int = id;                        i++;                        system.out.print(i);                   }              }               }             catch (exception e)                      {                         system.err.println("error: " + e.getmessage());                      }              }                    double total = (sup1 + bks1 + pap1);                  if(total >= 8000)                  {                     try                          {                                                                              filewriter fw1 = new filewriter("stars.txt", true);                                     printwriter pw1 = new printwriter(fw1);                          pw1.printf("%d %s %s %s $%.2f %s $%.2f %s $%.2f %s %s\r\n",                          id, first, last, "supplies", sup1, "books", bks1, "paper", pap1, dis, con);                          stardata1 = string.format("%d %s %s %s $%.2f %s $%.2f %s $%.2f %s %s\r\n",                          id, first, last, "supplies", sup1, "books", bks1, "paper", pap1, dis, con);                          star.append(stardata1);                          pw1.close();                            }                     catch(ioexception e)                          {                             system.err.println("error: " + e.getmessage());                          }                  }                         evalframe.setvisible(true);                          starframe.setvisible(true);                  }                            in.close();                          br.close();              }              catch (exception e)              {                 system.err.println("error: " + e.getmessage());              }                  } // end evaluate button            if (event.getsource() == exitbutton)          {              system.exit(0);          }       }// end of actionperformed     }// end of buttonhandler      }// end of dataentry class 


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -