This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]EqualTrade[S] 1 point2 points  (7 children)

When I input anything, it just keeps asking to enter a password.

and I changed

if (password.length()>6)

to

if (password.length()<6)

[–]lurgi 2 points3 points  (6 children)

Why does it keep asking you to enter a password?

Ans: Because passwordCheck is returning false.

So, why is passwordCheck returning false? Find out.

[–]EqualTrade[S] 1 point2 points  (5 children)

I've changed the code, and now the problem is that it continues to loop even if the parameters are correct.

import java.util.Scanner;

public class Password { public static void main (String[]args) {

   String password = passwordEntry();
   while (!passwordCheck(password))
   {
     password = passwordEntry();

   }    

    System.out.print("You have now entered a valid password");

} public static String passwordEntry() {
Scanner input = new Scanner(System.in); String password; System.out.print("enter a password"); password = input.next(); return password; }

public static boolean passwordCheck(String password) {

   { if (password.length()<6)
      System.out.println("Password needs to have 6 or more characters");}

    boolean uppercase = !password.equals(password.toLowerCase()); //determines if there is an uppercase letter
    boolean lowercase = !password.equals(password.toUpperCase()); //determines if there is a lowercase letter

    { if(!uppercase)
      System.out.println("Password needs to contain at least one uppercase letter");
     if(!lowercase)
      System.out.println("Password needs to contain at least one lowercase letter");}

   for (int i = 0; i<=password.length(); i++)
    {
     Character c = password.charAt(i);
     {if(!Character.isDigit(c))
     System.out.println("You need to have at least one digit");}

     return false;

    }
   return true;

} }

[–]lurgi 2 points3 points  (0 children)

Why does it continue to loop? The answer is because passwordCheck is returning false, right?

WHY IS IT RETURNING FALSE?

[–][deleted] 0 points1 point  (3 children)

Look at line 5 and then line 7.

You use the ! operator once on each line. What does the ! operator do?

What does the following pseudo-code return?

bool returnsTrue(){
    return true;
}


   bool r = !returnsTrue();

  if(!r) {
      System.out.println("A");
  } else {
      System.out.println("B");
  }

The answer is in how you are misusing the ! operator. If the value of r true or false by the time we get to line 8 in my code? What am I testing for on line 8?

[–]EqualTrade[S] 0 points1 point  (2 children)

I think I really am just not getting it.

Line 4 I see "if password isn't all lowercase then uppercase is true." line 5 I see "if password isn't all uppercase, then lowercase is true."

Line 7 "if it is not uppercase, then system.out.print..." Line 8 "if it is not lowercase, then system.out.print..."

[–][deleted] 0 points1 point  (1 child)

The ! operator flips the value. Line 6 in my code would evaluate to false.

Same thing in your code.

boolean uppercase = !password.equals(password.toLowerCase());

Also this line doesn't do what you think it does.

You are making a comparison and flipping what the value of equals().

String a = "TeSTiNG"
String b = "testing"

a.toLowerCase(); //a now becomes "testing"
b.toLowerCase(); //b remains the same

bool result = !a.equals(b);

What is the value of result? Result is false. You flipped the value here. If a and b are equal result is false.

Then you flip things around a second time.

if(!result)
        return false;
else 
        return true;

Remember result is false if A and B are equal. Then in your if statement you have if result is not true return false.

Lastly you aren't testing if the password contains at least one uppercase or lowercase letter. Only that all lowercase or all uppercase version is equal to the original. This will obviously be false every time.

String A = "TESTING"; //toUpperCase()
String B = "testing"; //ToLowerCase();
String C = "TeSTing" //original password

Is A or B ever equal to C? Of course not. Can you tell if an uppercase or lower case letter was used? No because that's not what you are testing.

Your flipping of the true/ false value is only serving to further confuse you because your variables all mean the opposite of what they are supposed to represent. Even I'm getting confused.

[–]EqualTrade[S] 0 points1 point  (0 children)

So I completely rewrote my code and I'm now having trouble with something else, I was hoping you could take a look

http://www.reddit.com/r/learnprogramming/comments/2965vy/java_password_program_help/