Skip to content

Instantly share code, notes, and snippets.

@0wlbear
Created January 25, 2016 17:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0wlbear/8e0d24abe8fdd7133c96 to your computer and use it in GitHub Desktop.
Save 0wlbear/8e0d24abe8fdd7133c96 to your computer and use it in GitHub Desktop.
Username / Password Validation from a Text File
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Scanner;
public class Lab1Frame extends JFrame
{
private static final int FRAME_WIDTH = 250;
private static final int FRAME_HEIGHT = 200;
private File userData;
static JTextField userNameField;
static JTextField passwordField;
public Lab1Frame() throws FileNotFoundException
{
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
private void createComponents() throws FileNotFoundException
{
userNameField = new JTextField(10);
passwordField = new JTextField(10);
JLabel userNameLabel = new JLabel("User Name");
JLabel passwordLabel = new JLabel("Password");
JButton loginButton = new JButton("Login");
JButton exitButton = new JButton("Exit");
JPanel panel = new JPanel();
panel.add(userNameField);
panel.add(userNameLabel);
panel.add(passwordField);
panel.add(passwordLabel);
panel.add(loginButton);
panel.add(exitButton);
add(panel);
ActionListener exitListener = new ClickListener1();
ActionListener loginListener = new ClickListener2();
exitButton.addActionListener(exitListener);
loginButton.addActionListener(loginListener);
}
public class ClickListener1 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
public class ClickListener2 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
File inputFile = new File("USERDATA.txt");
String userNameInput = userNameField.getText();
String passwordInput = passwordField.getText();
try {
Scanner in = new Scanner(new File("USERDATA.txt"));
while (in.hasNextLine())
{
String s = in.nextLine();
String[] sArray = s.split(",");
System.out.println(sArray[0]); //Just to verify that file is being read
System.out.println(sArray[1]);
if (userNameInput == sArray[0] && passwordInput == sArray[1])
{
JOptionPane.showMessageDialog(null,
"Login Successful", "Success",
JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,
"Invalid Username / Password Combo", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
in.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null,
"User Database Not Found", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
import javax.swing.JFrame;
import java.io.*;
public class Lab1Viewer
{
public static void main(String[] args) throws FileNotFoundException
{
JFrame frame = new Lab1Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
CharlieBrown,kdc*iJWw
Snoppy,3KnEgof9%
Linus,$R$ARo1g
PeppermintPatty,PD9wtik09+
Lucy,J!nyMdfp@7x
@eliseudr
Copy link

This helped me a lot, thanks man !!

@Duza-io
Copy link

Duza-io commented Jun 22, 2021

the file can't be read, why is that?

@shivamranabhat
Copy link

it reads only the first line of text file

Copy link

ghost commented Jan 26, 2022

It will always display the Invalid Username/Password Combo it cannot read the USERDATA

@crflorida
Copy link

I was able to get it to work successfully by putting the USERDATA.txt file directly into the target folder on the main project. Thanks for this works great!

@sumareaves
Copy link

instead of using ==
use userNameInput.equals(sArray[0] && passwordInput.equals(sArray[1]))
this will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment