0

This program always says "ERROR: PIN not correct" even I enter the "CORRECT" PIN,

I used "debugger" (IntelliJ) to see where mistakes are but I saw nothing. Debugger shows me they are same but anyway it shows me that they are not same things. Thanks in advance

public static void main (String args [])
{

    java.util.Scanner input = new java.util.Scanner (System.in);
    int acctNums [] = new int [] {12345 , 67890};
    String acctNames [] = new String []{"Mr Isk." , "Other"};
    String acctPINs [] = new String [] {"0101" , "9876"};
    double acctBalances [] = new double [] {10000.0 , 250.0};
    ATM.login(acctNums , acctNames , acctPINs , acctBalances);

}

 public static void login (int acctNums [] , String acctNames [] , String acctPINs [] , double acctBalances [])
{
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.println("Please enter your account number");
    int acctNum = input.nextInt();
    int index = 0;
    if(findAccount(acctNums , acctNum) == -1) {
        System.out.println("ERROR: Account not found");
        System.exit(0);
    }
    else
        for(index = 0 ; index<acctNums.length; index++)

            if(findAccount(acctNums , acctNum) == index)
                break;
    System.out.println("Please enter your PIN");
    String PIN = input.next();
    if(acctPINs[index] == PIN)
        System.out.println("Hello" + acctNames[index] + "What would you like to do today?");

    else

        System.out.println("ERROR: PIN not correct");
        System.exit(0);

}

GoksuBayy
  • 19
  • 4

1 Answers1

0

You compare 2 String by using ==, which compares the references and not the value of the String. Try to use the equals method for performing such a task.

acctPINs[index].equals(PIN)

Vincent Passau
  • 814
  • 8
  • 22