java - How to read a file in order to check if username has already been created -


so creating program called registerandlogin, have enter username, password , re-enter-password in order create new account. validation of users , passwords can done using:

1.a user name typed in during registration can not same of 5 names in username.txt.

2.also user name has between 5 , 8 characters without numbers (i.e., user name can [a-z] or [a-z] not digits [0-9]).

3.a password between 5 , 8 characters, numbers allowed (so 12345 , abcde both valid passwords).

4.the retyped password must same password, otherwise, error message output after hitting register button.

when user name passes 5 8 characters validity check, user name not exist in username.txt, , password passes validity check, new user name added username.txt.

so created username.txt 5 names. however, problem have not sure how read username.txt file can check whether username has been used or not.

code here, little long , appreciate.

import javax.swing.*; import java.io.*; import java.awt.event.*; import java.awt.*; import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception; import java.io.*; import java.util.scanner;  public class registerandlogin extends jframe  {   // variables sign screen private jpanel sup;  private jlabel suname, supw, suretypepw; private jtextfield sunametext, supwtext, suretypepwtext; private jbutton subutton;  public registerandlogin ()     {      super ("register , login");     signupscreen();  }  public void signupscreen () {     sup = new jpanel ();     // sup.setsize(50, 60);     setlayout (new flowlayout());     setcontentpane (sup);     suname = new jlabel ("name");     sunametext = new jtextfield (10);     supw = new jlabel ("password");     supwtext = new jtextfield (10);     suretypepw = new jlabel ("retype password");     suretypepwtext = new jtextfield (10);     subutton = new jbutton ("register");     sup.add (suname);     sup.add(sunametext);     sup.add(supw);     sup.add (supwtext);     sup.add (suretypepw);     sup.add (suretypepwtext);     sup.add(subutton);     sup.setvisible(true);     buttonhandler handlersu = new buttonhandler();     subutton.addactionlistener(handlersu); }  public void validatebyarray() {      string[] read1=null;             string[] read=null;     read1=files(read);      int minl = 5, maxl = 8;     string stname = sunametext.gettext();//username     string stpw = supwtext.gettext();//password     string strepw = suretypepwtext.gettext();//retype password       /******************************************************      *  validate user name                                   *      ******************************************************/      if(stname.length()< minl || stname.length() > maxl )  // check username length         system.out.println ("user name must between 5 , 8");     else      {             //check invalid characters in username         (int = 0 ; < stname.length(); ++)  // check invalid character             if (!                ((stname.charat (i)>= 'a' && stname.charat (i) <= 'z') ||                (stname.charat (i)>= 'a' && stname.charat (i) <= 'z')))             {                     system.out.println ("username contains invalid character");                      break;             }          // match names in array (file or database)         // note logic below works not secure since discloses         // information user names.         boolean unfound = false;         (int j = 0; j < 4; j++)             if (read1[j].equals(stname))             {                 system.out.println ("user name " + read1[j] + " exits");                 unfound = true;                 break;             }         //if    (!unfound)         //  system.out.println ("user name " + stname + "  created");                            }     system.out.println ("after un");     /******************************************************      *  validate password                                *      ******************************************************/       if(stpw.length()< minl || stpw.length() > maxl )  // check username length         system.out.println ("password must between 5 , 8");     else      {             //check invalid characters in username         (int = 0 ; < stpw.length(); ++)  // check invalid character             if (!                ((stpw.charat (i)>= '0' && stpw.charat (i) <= '9') ||                        (stpw.charat (i)>= 'a' && stpw.charat (i) <= 'z') ||                        (stpw.charat (i)>= 'a' && stpw.charat (i) <= 'z')))             {                     system.out.println ("password contains invalid character");                      break;             }          // note stname replaced stpw , strepw;         // un[] replaced pn[]         boolean unfound = false;              if (stpw.equals(strepw))             {                 system.out.println ("user name " + stname + "  created");                 unfound = true;              }         if  (!unfound)             system.out.println ("passwords not match");        }     //system.out.println ("after un again"); }      class buttonhandler implements actionlistener {     public void actionperformed (actionevent event){          if (event.getsource () == subutton){             system.out.println ("su button in register screen hit");              validatebyarray();         }     } }  public string[] files(string [] read)  {     try {            bufferedreader file1 = new bufferedreader(new filereader("usernames.txt"));                            //  scanner filereaderscan=new scanner(readtextfile);              //only want first 5 lines             for(int count=0;count<5;count++)             {                read[count] = file1.readline();  //use readline method            system.out.println(read[count]);             }         file1.close();   //close file      }       catch (exception e) {         // todo auto-generated catch block         e.printstacktrace();         system.out.println("oops");     }     return read; } public static void main(string[] args) {     // todo auto-generated method stub registerandlogin rl = new registerandlogin (); rl.setsize(200, 300); rl.setvisible(true); rl.setdefaultcloseoperation(jframe.exit_on_close); }  } 

you can below in read method

public string[] files()      throws filenotfoundexception {    list<string> existingusers = new arraylist<string>();    file users = new file("username.txt");    scanner sc = new scanner(users);    while (sc.hasnext()) {        existingusers.add(sc.nextline());    }    return existingusers.toarray(new string[existingusers.size()]);  } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -