How do I implement a login system in java using arrays
I did this in Visual Basic a couple of years back. What I'm doing here is creating a login system.
I have three users and three password respectively.
i.e. (Username) John - (Password) jMan21 , (Username) Mary - (Password) maree34 , (Username) Luke - (Password) skywalker.
At first I used the loop function but what it would do is cycle through all the arrays (obviously) and processed dialog boxes 3 times, one saying the login is successful and the other saying the login attempt has failed.
And I also used the "Array.equals" method.
This is the code I used:
Code :
package loginPackage;
import java.util.*;
import javax.swing.*;
public class userInterface extends javax.swing.JFrame {
String username="";
String password="";
String logZone;
public userInterface() {
initComponents();
}
private void jBtnEnterActionPerformed(java.awt.event.ActionEvent evt) {
username = jTxtFieldUser.getText();
password = jPassFieldPassword.getText();
logZone = (String) jCbxLogZone.getSelectedItem();
String[] usernameArray={"John","Mary","Luke"};
String[] passwordArray={"jMan21","maree34","skywalker"};
if(username.equals(usernameArray) && password.equals(passwordArray))
{
JOptionPane.showMessageDialog(null,"Welcome "+username+"\n"+"You are logged in as: "+logZone,"Log in",JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
}
}
Re: How do I implement a login system in java using arrays
Code :
if(username.equals(usernameArray) && password.equals(passwordArray))
The above code compares a String object with an array of Strings...and will evaluate to false. You will need to loop over the array values individually. You can do something like set a boolean flag indicating whether the entered values are correct. You might also want to consider a different data structure than parallel arrays (for example a class that describes a user)
Re: How do I implement a login system in java using arrays
package loginPackage;
import java.util.*;
import javax.swing.*;
public class userInterface extends javax.swing.JFrame {
String username="";
String password="";
String logZone;
public userInterface() {
initComponents();
}
private void jBtnEnterActionPerformed(java.awt.event.ActionEven t evt) {
username = jTxtFieldUser.getText();
password = jPassFieldPassword.getText();
logZone = (String) jCbxLogZone.getSelectedItem();
String[] usernameArray={"John","Mary","Luke"};
String[] passwordArray={"jMan21","maree34","skywalker"};
if((username.equals(usernameArray[i]) && password.equals(passwordArray[i])) || (username.equals(usernameArray[++i]) && password.equals(passwordArray[i]))||(username.equals(usernameArray[++i]) && password.equals(passwordArray[i])) )
{
JOptionPane.showMessageDialog(null,"Welcome "+username+"\n"+"You are logged in as: "+logZone,"Log in",JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
}
}
hope this will work fine for you
Re: How do I implement a login system in java using arrays
Please don't post junk code. You'll only confuse the OP.
Re: How do I implement a login system in java using arrays