So I started create this project for my PAT Grade 12 task and I hit a speedbump... I have a login screen, using a jFrame, in netbeans. My issue is, that I am using 2 text files to store the data: 1 for usernames and 1 for passwords. They are read in using a Scanner and are being delimited with a #. The point is, I validate the Username using a while loop, but i am unable to validate the password that correspond with that username in the other text file.
So you can imagine 2 text files:
- Username
- Password
When the 'Login' button is clicked, it uses a while loop to check to see if username already exists. If it does, continue to the Password, else "Username does not exist". The problem comes in when matching the Password, in the same position, in the other text file.
If anybody can help me, it would make my week and save my project. Please understand that Netbeans only lets you edit certain areas of code. I try my best to keep my code clean and understandable.
Many Thanks, SnowyTheVampire
package Battlefield4;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Login extends javax.swing.JFrame
{
String Username;
String Password;
public Login()
{
initComponents();
}
//Can not edit from here on
private void initComponents() {
txtUsername = new javax.swing.JTextField();
txtPassword = new javax.swing.JTextField();
lblPassword = new javax.swing.JLabel();
lblUsername = new javax.swing.JLabel();
btnLogin = new javax.swing.JButton();
btnCreate = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setSize(new java.awt.Dimension(854, 480));
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
getContentPane().add(txtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 150, 210, -1));
getContentPane().add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 220, 210, -1));
lblPassword.setText("Password");
getContentPane().add(lblPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 200, -1, -1));
lblUsername.setText("Username");
getContentPane().add(lblUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 130, -1, -1));
btnLogin.setText("Login");
btnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});
getContentPane().add(btnLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 290, -1, -1));
btnCreate.setText("Create");
btnCreate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCreateActionPerformed(evt);
}
});
getContentPane().add(btnCreate, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 290, -1, -1));
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Battlefield4/Batfield/Background/Login (Small).jpg"))); // NOI18N
jLabel1.setText("jLabel1");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 860, 480));
pack();
}
//Can not edit before this
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
//This is from where Netbeans lets you edit your code.
try {
File user = new File("C:\\Users\\John1012\\Documents\\NetBeansProjects\\PATProject\\src\\Battlefield4\\Batfield\\Users\\Username.txt"); //To create a universal file for Input & Output
File pass = new File("C:\\Users\\John1012\\Documents\\NetBeansProjects\\PATProject\\src\\Battlefield4\\Batfield\\Users\\Password.txt"); //To create a universal file for Input & Output
user.getParentFile().mkdirs(); //To denote the parent file
pass.getParentFile().mkdirs(); //To denote the parent file
Scanner scUser = new Scanner(user).useDelimiter("#"); //To scan the Username file
Scanner scPass = new Scanner(pass).useDelimiter("#");
Username = txtUsername.getText(); //This gets the user input
Password = txtPassword.getText(); //This gets the user input
int pos = 0; //Indicates the position of the Username in the save file
while(scUser.hasNext())
{
pos = pos + 1;
if(scUser.equals(Username))
{ //This is where it fails
for (int i = 0; i <= 5; i++)
{
while(scPass.hasNext())
{
System.out.print(scPass);
}
}
if(scPass.equals(Password))
{
new Selection().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null,"Incorrect Password!");
}
}//This is where it works from. The above code is sketchy
else
{
JOptionPane.showMessageDialog(null,"User Does Not Exist!");
}
scUser.close();
}
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {
//Again this is from where Netbeans lets you edit
try {
File user = new File("C:\\Users\\John1012\\Documents\\NetBeansProjects\\PATProject\\src\\Battlefield4\\Batfield\\Users\\Username.txt"); //To create a universal file for Input & Output
File pass = new File("C:\\Users\\John1012\\Documents\\NetBeansProjects\\PATProject\\src\\Battlefield4\\Batfield\\Users\\Password.txt"); //To create a universal file for Input & Output
user.getParentFile().mkdirs(); //To denote the parent file
pass.getParentFile().mkdirs(); //To denote the parent file
Scanner scUser = new Scanner(user); //To scan the Username file
Username = txtUsername.getText(); //This gets the user input
Password = txtPassword.getText(); //This gets the user input
if(scUser.nextLine().contains(Username)) //This reads the entire line and checks for an indexOf of those characters
{
JOptionPane.showMessageDialog(null,"User Already Exists!");
scUser.close(); //Close the Scanner
}
else
{
scUser.close(); //Close the Scanner
if("".equals(Password) || " ".equals(Password)) //Checks to see whether or not the user entered a password
{
JOptionPane.showMessageDialog(null,"Please Enter A Password!");
}
else
{
PrintWriter pwUser = new PrintWriter(new FileWriter(user,true)); //To Write to the Username file
PrintWriter pwPass = new PrintWriter(new FileWriter(pass,true)); //To Write to the Password file
pwUser.print(Username + "#");
pwPass.print(Password + "#");
pwUser.close();
pwPass.close();
new Selection().setVisible(true);
this.dispose();
}
}
scUser.close();
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
//Can no longer edit here
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnCreate;
private javax.swing.JButton btnLogin;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblPassword;
private javax.swing.JLabel lblUsername;
private javax.swing.JTextField txtPassword;
private javax.swing.JTextField txtUsername;
// End of variables declaration
}