Created
January 25, 2016 17:40
-
-
Save 0wlbear/8e0d24abe8fdd7133c96 to your computer and use it in GitHub Desktop.
Username / Password Validation from a Text File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CharlieBrown,kdc*iJWw | |
Snoppy,3KnEgof9% | |
Linus,$R$ARo1g | |
PeppermintPatty,PD9wtik09+ | |
Lucy,J!nyMdfp@7x |
the file can't be read, why is that?
it reads only the first line of text file
It will always display the Invalid Username/Password Combo it cannot read the USERDATA
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!
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
This helped me a lot, thanks man !!