By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,677 Members | 1,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,677 IT Pros & Developers. It's quick & easy.

Simple ATM Scenario --> Having trouble with do-while loop

P: 5
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.

Expand|Select|Wrap|Line Numbers
  1. public class ATM {
  2.  
  3.     public static Scanner kbd = new Scanner (System.in);
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         int choice, count = 1;
  8.         double balance;
  9.         String acctNum, pwd, check;
  10.  
  11.         do{
  12.             System.out.print("Enter your account number: ");
  13.             acctNum = kbd.nextLine();
  14.             System.out.print("Enter your account's password: ");
  15.             pwd = kbd.nextLine();
  16.  
  17.             check = checkID(acctNum, pwd); 
  18.  
  19.             if (check == "error"){
  20.                 System.out.println("\nError: Account number and/or password is incorrect.\n");
  21.                 count++;
  22.             }
  23.         }while (count <= 3 && count > 1);    
  24.  
  25.         if (count > 3){
  26.             System.out.println("\nYou have reached the maximum attempts allowed.");
  27.             return;
  28.         }
  29.  
  30.         else{
  31.  
  32.             balance = Double.parseDouble(check);
  33.             choice = menu();
  34.             if (choice == 0){
  35.                 System.out.print("Error: Select menu item 1-4");
  36.                 choice = menu();}
  37.             else if (choice == 1)
  38.                 displayBalance(balance);
  39.             else if (choice == 2)
  40.                 balance = deposit(balance);
  41.             else if (choice == 3)
  42.                 balance = withdraw(balance);
  43.         }
  44.     }
  45.  
  46. // The checkID method determines if acctNum is a valid account number
  47. // and pwd is the correct password for the account.  If the account information
  48. // is valid, the method returns the current account balance, as a string.
  49. // If the account information is invalid, the method returns the string "error".
  50.  
  51. public static String checkID(String acctNum, String pwd){
  52.  
  53.     String result = "error";
  54.  
  55.     // Strings a, b, and c contain the valid account numbers and passwords.
  56.     // For each string, the account number is listed first, followed by
  57.     // a space, followed by the password for the account, followed by a space,
  58.     // followed by the current balance.
  59.  
  60.     String a = "44567-5 mypassword 520.36";
  61.     String b = "1234567-6 anotherpassword 48.20";
  62.     String c = "4321-0 betterpassword 96.74";
  63.  
  64.     // insert code here to determine if acctNum is a valid account number
  65.     // and pwd is the correct password for the account.
  66.     String acctnumA, acctnumB, acctnumC, pwdA, pwdB, pwdC;
  67.  
  68.     acctnumA = a.substring(0, a.indexOf(' '));
  69.     pwdA = a.substring(a.indexOf(' ')+1, a.lastIndexOf(' '));
  70.     acctnumB = b.substring(0, b.indexOf(' '));
  71.     pwdB = b.substring(b.indexOf(' ')+1, b.lastIndexOf(' '));
  72.     acctnumC = c.substring(0, c.indexOf(' '));
  73.     pwdC = c.substring(c.indexOf(' ')+1, c.lastIndexOf(' '));
  74.  
  75.     if (acctnumA == acctNum && pwdA == pwd){
  76.         return a.substring(a.lastIndexOf(' ')+1);
  77.     } else if (acctnumB == acctNum && pwdB == pwd){
  78.         return b.substring(b.lastIndexOf(' ')+1);
  79.     } else if (acctnumC == acctNum && pwdC == pwd){
  80.         return c.substring(c.lastIndexOf(' ')+1);
  81.     } else {
  82.         return result;}
  83. }
  84.  
Mar 26 '09 #1
Share this Question
Share on Google+
2 Replies


P: 5
I said for loop up there a few times i meant do-while...sorry
Mar 26 '09 #2

10K+
P: 13,264
Do not compare strings using == . Use the .equals method instead.
Mar 27 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.