I am trying to create a java program which compares the stored string(meaning password stored) and another string to check password. and also trying to add a number of attempts like 3 in it. am almost done. but am still getting one small logical error. i tried so many changes and adding break statements to my knowledge but still the program isn't becoming perfect. if i change something then another logical error comes. everything works fine but this extra line occurs if i put right pass and right amount which i don't want."Account is blocked. Contact Bank Manager". how can i stop getting that. hope you guys will help me!
public class Test {
int otherBankUserId=345;
otherBankUserPass="suresh";
int otherAcc;String otherPass=null;
Scanner sc = new Scanner(System.in);
public void checkOtherPass() {
System.out.println("Enter the account");
otherAcc = sc.nextInt();
if(otherAcc==otherBankUserId)
{
while(passError<3) {
System.out.println("Account number is verified. Enter the password");
otherPass = sc.next();
if(otherPass.equals(otherBankUserPass)) {
System.out.println("logged in successfully");
System.out.println("Enter the amount:");
int amountSum=sc.nextInt();
System.out.println(amountSum+"Rs withdraw successfully done. Remove the card!");
}
else {
System.out.println("wrong password!");
passError=passError+1;
System.out.println("Wrong Entries:"+passError+"\nMax Wrong pass entries:3");
}
}
System.out.println("Account is blocked. Contact Bank manager");
}
else {
System.out.println("Wrong account number.Do the process again");
}
}
public static void main(String[] args) {
Test t = new Test();
t.checkOtherPass();
}
}
output:
Enter the account
345
Account number is verified. Enter the password
suresh
logged in successfully
Enter the amount:
333
333 withdraw successfully done. Remove the card!
Account is blocked. Contact Bank manager
Everything works fine but this extra line occurs if i put right pass and right amount which i don't want."Account is blocked. Contact Bank Manager". how can i stop getting that
You are printing the line you do not want after the loop is done regardless if the password was correct or not. It would help to format your code better with proper indentation to better see the problem.
There are several possible ways to solve this problem. Below, is an example using the value of passError. Note that the value of this variable gets set to -1 when the correct password is given. Also note that your passError variable isn't declared anywhere in the provided code.
if (otherAcc == otherBankUserId) {
int passError = 0;
while (passError >= 0 && passError < 3) {
System.out.println("Account number is verified. Enter the password");
otherPass = sc.next();
if (otherPass.equals(otherBankUserPass)) {
passError = -1;
System.out.println("logged in successfully");
System.out.println("Enter the amount:");
int amountSum=sc.nextInt();
System.out.println(amountSum+"Rs withdraw successfully done. Remove the card!");
} else {
System.out.println("wrong password!");
passError += 1;
System.out.println("Wrong Entries:"+passError+"\nMax Wrong pass entries:3");
}
} if (passError < 3) {
System.out.println("Thank you for your business");
} else {
System.out.println("Account is blocked. Contact Bank manager");
}
} else {
System.out.println("Wrong account number. Do the process again");
}