java - Why am I getting a false value at if (users.contains(user))? -


i think user.contains not reading every line , checking first line. had working right earlier(i testing duplicate user portion of code), program skipping :

{                 joptionpane.showmessagedialog(null, "duplicate user found.");              goahead=false;             dispose();             } 

i not sure did, or how managed break own program. skipping way to:

else { if (hs.contains(new string(un+" "+pw))) {                  joptionpane.showmessagedialog(null,"user, found access granted!");                     dispose();                 } 

where did go wrong?

private void submitactionperformed(java.awt.event.actionevent evt) {                                            string un = username.gettext().trim();         string pw = password.gettext().trim();         hashset hs= new hashset();         hashset users = new hashset();          boolean goahead=true;          try {             scanner scan = new scanner(new file("login.txt"));             while (scan.hasnextline()) {                  string authenticator = scan.nextline().trim();                 string[] autparts=authenticator.split(" ");                  string user = autparts[0].trim();                 if (goahead){                     if (users.contains(user)) {                         if (user.equals(un)) {                                 joptionpane.showmessagedialog(null, "duplicate user found.");                              goahead=false;                             dispose();                         }                     } else {                         hs.add(authenticator);                         users.add(user);                     }                 }             }         } catch (exception ex) {              ex.printstacktrace();          }          if (goahead) {             if (createaccount.isselected() & (hs.contains(new string(un+" "+pw)))){                 joptionpane.showmessagedialog(null,"user exsist! no need create new account. ");                 dispose();             } else {                  if (hs.contains(new string(un+" "+pw))) {                      joptionpane.showmessagedialog(null,"user, found access granted!");                     dispose();                 }  else {                      if (createaccount.isselected()){                         try {                             printwriter output = new printwriter(new bufferedwriter(new filewriter("login.txt", true)));                              output.println(un+" "+pw);                             output.close();                          } catch (ioexception ex) {                             system.out.printf("error %s/n", ex );                         }                            joptionpane.showmessagedialog(null,"welcome!"+" " + un+" "+"please relogin now");                         dispose();                     }else {                         joptionpane.showmessagedialog(null, "user doesn't exist or password incorrect. ");                          dispose();                     }                 }             }         } 

the following output , whats in txt file. :screen shot of debug results , txt file

i have formatted code now:

string un = username.gettext().trim(); if (goahead){        if (users.contains(user))         {          if (user.equals(un))           {                  joptionpane.showmessagedialog(null, "duplicate user found.");               goahead=false;              dispose();          }        }         else         {         hs.add(authenticator);         users.add(user);        } } 

now when flag goahead set false dont need reset true somewhere? if hashset contains user why need again compare name of user? should have defined equals user itself. nope?


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -