I'm making a little project just to practice arrays/loops. It's basically a bank page that you can sign up and log into. I want to be able to store the user inputs of their username and password into the array lists when they sign up, then when the user logs in - the system will check if the username matches a name in the database (users array list) or if the password matches (passes array list). However, I discovered that the variables aren't being stored in the arrays when I tried testing it by printing out the array list. How do I properly store the user values into the arrays so I can use them later on? Hopefully that all made sense. Thank you.
import javax.swing.*;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ImageIcon icon = new ImageIcon("cutepig.jpg");
String[] response1 = {"Sign up", "Log in", "Exit"};
int options01 = JOptionPane.showOptionDialog(null, "Welcome to Beanie's Bank!", "Beanie's Bank", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, icon, response1, 0);
ArrayList<String> users = new ArrayList<>();
ArrayList<String> passes = new ArrayList<>();
while (true) {
if (options01 == 0) {
JTextField username = new JTextField();
users.add(username.getText());
JTextField password = new JPasswordField();
passes.add(password.getText());
JTextField confpass = new JPasswordField();
Object[] message = {
"Username: ", username,
"Password: ", password,
"Confirm Password: ", confpass,
};
int signup1 = JOptionPane.showConfirmDialog(null, message, "Sign up", JOptionPane.OK_CANCEL_OPTION);
if (signup1 == JOptionPane.OK_OPTION) {
if (password.getText().equals(confpass.getText()) && !password.getText().isEmpty() && !confpass.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Sign up was successful! \n Welcome to Beansie's Bank. \n Please log in to access your account!");
break;
} else if (password.getText().isEmpty() || confpass.getText().isEmpty() || username.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Input cannot be empty, please enter a valid input.");
continue;
} else if (!password.getText().equals(confpass.getText())) {
JOptionPane.showMessageDialog(null, "Your passwords do not match! Please try again.");
continue;
}
}
}
}
while(true){
JTextField username = new JTextField();
JTextField password = new JPasswordField();
Object[] message = {
"Username: ", username,
"Password: ", password
};
int option = JOptionPane.showConfirmDialog(null, message, "login", JOptionPane.OK_CANCEL_OPTION);
if (username.getText().equals(users) && password.getText().equals(passes)) {
JOptionPane.showMessageDialog(null, "Login successful, welcome back " + username + ".");
break;
} else if (!username.getText().equals(users) && !password.getText().equals(passes)){
JOptionPane.showMessageDialog(null,"Login failed, please try again.");
continue;
}
else{
System.out.println("Login canceled");
continue;
}
}
}}
The username
and password
fields are being added to the users
and passes
lists before the user has a chance to input their credentials so the fields were empty at that time. You should move it to after checking for the OK_OPTION
.
while (true) {
if (options01 == 0) {
JTextField username = new JTextField();
JTextField password = new JPasswordField();
JTextField confpass = new JPasswordField();
Object[] message = {
"Username: ", username,
"Password: ", password,
"Confirm Password: ", confpass,
};
int signup1 = JOptionPane.showConfirmDialog(null, message, "Sign up", JOptionPane.OK_CANCEL_OPTION);
if (signup1 == JOptionPane.OK_OPTION) {
if (password.getText().equals(confpass.getText()) && !password.getText().isEmpty() && !confpass.getText().isEmpty()) {
users.add(username.getText());
passes.add(password.getText());
JOptionPane.showMessageDialog(null, "Sign up was successful! \n Welcome to Beansie's Bank. \n Please log in to access your account!");
break;
} //... rest of your code
}
}
}