-5

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;
        }
}
midrigs
  • 65
  • 2
  • 9
  • Why on earth are you doing a character-by-character comparison of two `String`s instead of just using `equals()`? – azurefrog Jul 22 '14 at 16:20
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Zeus Jul 22 '14 at 16:26
  • Sorry for this question, obvious answer!!! Didn't get much sleep the last couple nights and trying to code today is not turning out very well... – midrigs Jul 22 '14 at 16:37

3 Answers3

0

You could simplify it by using equals() instead of your compare function.

Example : pInput.equals(pInput2);
Zeus
  • 432
  • 7
  • 19
0

As the other poster said, .equals() is much simpler and returns the same results and it doesnt involve the tedious use of char arrays unnecessarily.

Also, your for loop in the compare function should be <, not >=. It will return an exception otherwise.

KM529
  • 372
  • 4
  • 17
0

You have a logic error in your compare() method.

When you use one = you are doing an assignment, not a comparison:

if (passCompare = true) // assigns true to passCompare and always evaluates true
    return true;
else
    return false;

More fundamentally, don't reinvent the wheel. The String class already has a perfectly good way to check to see if two Strings are equal: the equals() method.

Instead of this:

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.");
}

...you could instead do this:

    do {
        pInput2 = JOptionPane.showInputDialog(null, "Please re-enter your password: \n");
    } while (!pInput.equals(pInput2));

    if (pInput.equals(pInput2)) {
        //if input is validated and returns true- finish program
        JOptionPane.showMessageDialog(null, "Your password was successfully entered.");
    }

This would let you do away with your compare() method altogether, which removes to opportunity to make mistakes in it.

azurefrog
  • 10,785
  • 7
  • 42
  • 56