I am having a problem with the validation of the account number and password.
The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID method that checks the account number and pwd with 3 stored accounts (each account/pwd/balance is stored as one long string) so the checkID method breaks up the string into its seperate parts and then checks if the input matches. If it does match one of the accounts it returns the balance of the account as a string. If not it returns an error string.
My problem is that when I enter a correct account/ pwd it continues the for loop even though the while loop conditions has been met. So, it just keeps asking me to input a account number then password until max attempts has been reached. So either there is a problem in my checkID method or in the if statement that activates the counter for he for-while loop. I just cant seem to spot my error. Here is the main method and checkID method in my program. Can someone help find my error or at least point me in the right directon.
The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID method that checks the account number and pwd with 3 stored accounts (each account/pwd/balance is stored as one long string) so the checkID method breaks up the string into its seperate parts and then checks if the input matches. If it does match one of the accounts it returns the balance of the account as a string. If not it returns an error string.
My problem is that when I enter a correct account/ pwd it continues the for loop even though the while loop conditions has been met. So, it just keeps asking me to input a account number then password until max attempts has been reached. So either there is a problem in my checkID method or in the if statement that activates the counter for he for-while loop. I just cant seem to spot my error. Here is the main method and checkID method in my program. Can someone help find my error or at least point me in the right directon.
Code:
public class ATM {
public static Scanner kbd = new Scanner (System.in);
public static void main(String[] args) {
int choice, count = 1;
double balance;
String acctNum, pwd, check;
do{
System.out.print("Enter your account number: ");
acctNum = kbd.nextLine();
System.out.print("Enter your account's password: ");
pwd = kbd.nextLine();
check = checkID(acctNum, pwd);
if (check == "error"){
System.out.println("\nError: Account number and/or password is incorrect.\n");
count++;
}
}while (count <= 3 && count > 1);
if (count > 3){
System.out.println("\nYou have reached the maximum attempts allowed.");
return;
}
else{
balance = Double.parseDouble(check);
choice = menu();
if (choice == 0){
System.out.print("Error: Select menu item 1-4");
choice = menu();}
else if (choice == 1)
displayBalance(balance);
else if (choice == 2)
balance = deposit(balance);
else if (choice == 3)
balance = withdraw(balance);
}
}
// The checkID method determines if acctNum is a valid account number
// and pwd is the correct password for the account. If the account information
// is valid, the method returns the current account balance, as a string.
// If the account information is invalid, the method returns the string "error".
public static String checkID(String acctNum, String pwd){
String result = "error";
// Strings a, b, and c contain the valid account numbers and passwords.
// For each string, the account number is listed first, followed by
// a space, followed by the password for the account, followed by a space,
// followed by the current balance.
String a = "44567-5 mypassword 520.36";
String b = "1234567-6 anotherpassword 48.20";
String c = "4321-0 betterpassword 96.74";
// insert code here to determine if acctNum is a valid account number
// and pwd is the correct password for the account.
String acctnumA, acctnumB, acctnumC, pwdA, pwdB, pwdC;
acctnumA = a.substring(0, a.indexOf(' '));
pwdA = a.substring(a.indexOf(' ')+1, a.lastIndexOf(' '));
acctnumB = b.substring(0, b.indexOf(' '));
pwdB = b.substring(b.indexOf(' ')+1, b.lastIndexOf(' '));
acctnumC = c.substring(0, c.indexOf(' '));
pwdC = c.substring(c.indexOf(' ')+1, c.lastIndexOf(' '));
if (acctnumA == acctNum && pwdA == pwd){
return a.substring(a.lastIndexOf(' ')+1);
} else if (acctnumB == acctNum && pwdB == pwd){
return b.substring(b.lastIndexOf(' ')+1);
} else if (acctnumC == acctNum && pwdC == pwd){
return c.substring(c.lastIndexOf(' ')+1);
} else {
return result;}
}
Comment