title: verifying username/password from text file in java
body: <p>I am attempting to create a very basic login system that verifies the username and password that the user enters to the ones stored in a text file. I have looked at other examples on this website and other places but I have been unable to get them to work properly. I know that text files are not secure, but this is just for learning purposes and will not be implemented in anything serious.</p>
  <p>The text file it is reading contains these two lines in the format &quot;username,password&quot;</p>
  <blockquote>
  <p>student1,student1</p>
  <p>student2,student2</p>
  </blockquote>
  <p>The goal is for the If statement to verify the login credentials and then open the next frame. I haven't added the code for that part yet because I have been unable to debug everything up to that.</p>
  <p>Here is the code:</p>
  <pre><code>btnLogin.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
             File loginf = new File(&quot;users.txt&quot;);
  
                  try{
                      Scanner read = new Scanner(loginf);
                      read.useDelimiter(&quot;,&quot;);
                      boolean login = false;
                      while(read.nextLine() !=null){
                             String user = read.next();
                             String pass = read.next();
                             read.next();
                             if(username.getText().equals(user) &amp;&amp; passwordField.getText().equals(pass)){
                                login = true;
                                break;                 
                             }
                          }
                          if(login)
                             System.out.println(&quot;good&quot;); //using for testing purposes
                          else {
                             JOptionPane.showMessageDialog(null, &quot;Incorrect username or password&quot;);
                             username.setText(&quot;&quot;);
                             passwordField.setText(&quot;&quot;);
                          }
                      read.close();
                  }
                  catch (FileNotFoundException qwerty){
                          JOptionPane.showMessageDialog(null, &quot;Can't find a text file&quot;);
                  }
             }
          });
  </code></pre>
  <p>Thanks for all of your help in advance.</p>