I am trying to create a program required for my homework, here is the professors question:
"Write an application that accepts a user's password from an input dialogs. When the entered password is less than six characters, more than 10 characters, or does not contain at least one letter and one digit, prompt the user again. When the user's entry meets all the password requirements, prompt the user to reenter the password, and do not let the user continue until the second password matches the first one."
Everything works up until it tries to compare the two passwords entered... I do not know whats going on and why it won't compare correctly.
Here is my code:
import javax.swing.*;
public class Password {
public static void main(String[] args) {
//
String pInput = "";
String pInput2 = "";
int i = 0;
//do-while loop to obtain password from input and verify input meets requirements in authenticate method.
do {
pInput = JOptionPane.showInputDialog(null, "Please enter your password.\n"
+ "Your password must have 6-10 characters\n"
+ "Your password must contain at least one letter and one digit");
}
while (authenticate(pInput) == false);
do {
pInput2 = JOptionPane.showInputDialog(null, "Please re-enter your password: \n");
}
while (compare(pInput, pInput2) == false);
if (compare(pInput, pInput2) == true) {
//if input is validated and returns true- finish program
JOptionPane.showMessageDialog(null, "Your password was successfully entered.");
}
}
private static boolean compare(String pass1, String pass2) {
// TODO Auto-generated method stub
char[] passArray1 = pass1.toCharArray();
char[] passArray2 = pass2.toCharArray();
boolean passCompare = false;
int x = 0;
for(int i = 0; i >= pass1.length(); i++) {
if (passArray1[i] == passArray2[i]) {
x++;
}
}
if (x == pass1.length()) {
passCompare = true;
}
else passCompare = false;
if (passCompare = true)
return true;
else
return false;
}
private static boolean authenticate(String password)
{
// if password is not six characters long or greater than 10 return false - else continue.
if (password == null || password.length() < 6 || password.length() > 10) {
return false;
}
boolean containsChar = false;
boolean containsDigit = false;
//for loop that passes password to an array which finds if array contains a character and digit.
for (char c : password.toCharArray()) {
if (Character.isLetter(c)) {
containsChar = true;
} else if (Character.isDigit(c)) {
containsDigit = true;
}
if (containsChar && containsDigit) {
return true;
}
}
return false;
}
}