473,569 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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
2 7612
cmb3587
5 New Member
I said for loop up there a few times i meant do-while...sorry
Mar 26 '09 #2
r035198x
13,262 MVP
Do not compare strings using == . Use the .equals method instead.
Mar 27 '09 #3

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

Similar topics

1
2577
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i access the recordset it displays the results, what the real issue is that the entry is not made into the database even though i use the Update...
2
2518
by: ed | last post by:
i'm having trouble with a form. I want to be able to type in the address of the form with the data for the form items in the URL (ie: http://somesite.com/formpage.html?field1=data1&field2=data2). It saves the data if I type it in manually to an html file. But it won't do that if I use the URL notation above. How do I get it to do this. ...
1
1789
by: Lauren Wilson | last post by:
I'm having trouble with the Access VBA help on my installation of A2K with Dev tools. Every time I try to retrieve help for items listed in the Object Browser (and SOME other items as well), Access tells me that the "feature is broken" and prompts me to repair it. When I do so, it goes through the motions but does NOT repair the help files....
2
4533
by: Jozef | last post by:
Hello, I am trying to put together a module and open a workspace on a database that has a simple password (using Access XP). This is the lin that I'm having trouble with; Set wrk = CreateWorkspace("TestWrkspc", "Admin", conDbPwd) conDBPwd is a variable that contains the password. There is no independant workgroup file, just the...
0
1526
by: Jozef | last post by:
Hello, I'm having trouble with the download links on my web server. The error I'm getting is; CGI Timeout The specified CGI application exceeded the allowed time for processing. The server has deleted the process. It's a fresh Windows 2000 server install, but I also installed the ASP.net
1
1612
by: Jozef | last post by:
Hello. I'm having trouble creating a blank solution (and ASP.net web application) from my laptop. I own the server (in fact it's sitting right next to me) and have added the URL to the trusted sites on my laptop. Here are the details; This is what I'm selecting from the start page.... >Add New Blank Solution >Visual Basic Projects,
1
5137
by: MLH | last post by:
Am having trouble with the filter property setting below. Would like to filter the listing to car makes beginning with "D". I'm blowing it on the filter spec somehow??? Sub OpenRecordsetX() Dim MyDB As Database Dim rstTemp As Recordset Dim rstTemp2 As Recordset Set MyDB = CurrentDb()
5
3488
by: tkondal | last post by:
Hi all. I just started looking at Python's ctypes lib and I am having trouble using it for a function. For starters, here's my Python code: from ctypes import*; myStringDLL= cdll.LoadLibrary("myStringDLL.dll");
2
2011
by: Stu | last post by:
Hi guys, I've been having trouble getting the clock function to work portably, please could I get some thoughts? <Possibly OT comments> It works fine on my laptop (under WinXP) and on my office computer (under Linux), but I have to write some code for the system simulator for the Cell BE processor (the thing inside the PS3), which is...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.