1

this i my first attempt at asking a question so hopefully it shows correctly. Basically what I need the program to do is to ask the user for a preset account number and password and only allow them 3 attempts. I then want to call up another method when both requirements are met so i can continue with the program. The first problem i have is that when i enter the correct password its is still showing as incorrect and i don't know why, then i would like to know if i have call the method within the if statement correctly. Thanks.

import java.util.Scanner;


public class Part4 {

public static void main(String[] args) 
{

    String password = "password", passwordattempt = null;
    int accnum = 123456789, acctry = 0, tries = 0;
    Scanner input = new Scanner (System.in);


    while (acctry != accnum){
    System.out.println("\nPlease enter your account number");
    acctry = input.nextInt();

        if (acctry != accnum)
            System.out.print("That number is incorrect. Please try again.");

        else
            if (acctry == accnum)
            {


                while (tries < 3)
                {


                    System.out.println("\nPlease enter password");
                    passwordattempt = input.next();


                    if (passwordattempt != password){
                        System.out.print("That password is incorrect");
                        tries++;
                        }
                    else
                        if (passwordattempt == password){
                            System.out.print("That is correct");
                            AccountDetails.Details(args);
                            }
                }

                System.out.print("\nYou have exceeded the ammount of tries");

            }               


        }

}

public static class AccountDetails {
    private static void Details(String[] args){
        System.out.print("it works");
    }
}

}
John
  • 11
  • 1
  • Please learn how to compare strings. – SLaks Dec 09 '13 at 18:23
  • Just as an addendum to what SLaks said, read up on object reference equality vs what we perceive as equality. http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/513844#513844 – BoredBlazer Dec 09 '13 at 18:27

2 Answers2

2

two problems.

  • 1: You're executing your while loop regardless of if it is successful or not.

.

while(tries < 3)

should be

while(tries < 3 && !successfulPassword)

You'll need to add the successfulPassword variable, so that you don't get it right the first time and yet continue to have to enter passwords.

  • 2: Your comparison of strings is grossly, umm, well, wrong. There's two things that catch my eye. The first is you can't use == and != and get the results you expect. You must use .equals(). Secondly, you don't need to repeat the opposite clause like you do with a human. For example, I tell my daughter "If you eat your supper, then you may have cookies. Else, if you do not eat your supper, then you may not have cookies." To a computer, you don't need that last "if you do not eat your supper". It's guaranteed to be true (since you're in the else block anyway) and it just clutters it up. So that just becomes

.

if(passwordAttempt.equals(password) {
    successfulPassword = true;
} else {
    tries++;
}
corsiKa
  • 81,495
  • 25
  • 153
  • 204
0

In the Java language, Strings are objects, and thus comparing them using '==' is testing by reference, and not by equality.

I believe what you are looking for is

if (passwordattempt.equals(password)) {

Check here for more information:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#equals(java.lang.Object)

Michael Parker
  • 12,724
  • 5
  • 37
  • 58