title: How to read a file in order to check if username has already been created
body: <p>So I am creating a program called RegisterandLogin, which basically you have to enter a username, password and re-enter-password in order to create a new account. 
  Validation of users and passwords can be done using:</p>
  
  <p>1.A user name you typed in during registration can not be the same as any 
  of the 5 names in username.txt. </p>
  
  <p>2.Also the user name has to be between 5 and 8 characters without any numbers (i.e., the user name can be only [A-Z] or [a-z] but not any digits like [0-9]).</p>
  
  <p>3.A password is also between 5 and 8 characters, with numbers allowed (so 12345 and abcde are both valid passwords).</p>
  
  <p>4.The retyped password must be the same as password, otherwise, an error message is output after hitting the Register button.         </p>
  
  <p>When a user name passes the 5 to 8 characters validity check, user name does not exist
  in username.txt, and Password passes the validity check, the new user name is added
  to username.txt.</p>
  
  <p>So I already created the username.txt with five names. However, the problem I have is I am not sure how to read the username.txt file so it can check whether the username has been used or not.</p>
  
  <p>Code here, little long , any help will be appreciate.</p>
  
  <pre><code>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 for Sign up screen
  private JPanel suP; 
  private JLabel suName, suPW, suRetypePW;
  private JTextField suNameText, suPWText, suRetypePWText;
  private JButton suButton; 
  public RegisterandLogin ()
      { 
      super ("Register and 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()&lt; minL || stName.length() &gt; maxL )  // Check username length
          System.out.println ("User name must be between 5 and 8");
      else 
      {
              //check invalid characters in username
          for (int i = 0 ; i &lt; stName.length(); i ++)  // Check for invalid character
              if (!
                 ((stName.charAt (i)&gt;= 'A' &amp;&amp; stName.charAt (i) &lt;= 'Z') ||
                 (stName.charAt (i)&gt;= 'a' &amp;&amp; stName.charAt (i) &lt;= 'z')))
              {
                      System.out.println ("Username contains invalid character"); 
                      break;
              }
  
          // Match the names in the array (file or database)
          // Note the logic below works but is NOT secure since it discloses
          // information about user names.
          boolean uNfound = false;
          for (int j = 0; j &lt; 4; j++)
              if (read1[j].equals(stName))
              {
                  System.out.println ("User name " + read1[j] + " already exits");
                  uNfound = true;
                  break;
              }
          //if    (!uNfound)
          //  System.out.println ("User name " + stName + "  created");                       
      }
      System.out.println ("After UN");
      /******************************************************
       *  Validate password                                *
       ******************************************************/
  
  
      if(stPW.length()&lt; minL || stPW.length() &gt; maxL )  // Check username length
          System.out.println ("Password must be between 5 and 8");
      else 
      {
              //check invalid characters in username
          for (int i = 0 ; i &lt; stPW.length(); i ++)  // Check for invalid character
              if (!
                 ((stPW.charAt (i)&gt;= '0' &amp;&amp; stPW.charAt (i) &lt;= '9') ||
                         (stPW.charAt (i)&gt;= 'A' &amp;&amp; stPW.charAt (i) &lt;= 'Z') ||
                         (stPW.charAt (i)&gt;= 'a' &amp;&amp; stPW.charAt (i) &lt;= 'z')))
              {
                      System.out.println ("Password contains invalid character"); 
                      break;
              }
  
          // Note stName replaced by stPW and stRePW;
          // uN[] replaced by pN[]
          boolean uNfound = false;
  
              if (stPW.equals(stRePW))
              {
                  System.out.println ("User name " + stName + "  created");
                  uNfound = true;
  
              }
          if  (!uNfound)
              System.out.println ("Passwords does 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&lt;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);
  }
  
  }
  </code></pre>