'Limit login attempt in Java
I am trying to limit the maximum login attempts to 3. However, my code below uses all the attempts before the user can get a chance to press the login button again. How would I fix this?
private void executeLogin() {
String userNameStr = userNameTF.getText();
String passWordStr = passWordTF.getText();
int totalAttempts = 3;
while (totalAttempts != 0) {
if (userNameStr == "temp" && passWordStr == "pass") {
System.out.println("Login Correct!");
return;
} else {
System.out.println("Incorrect Login");
totalAttempts--;
System.out.println(totalAttempts);
}
}
if (totalAttempts == 0) {
System.out.println("Maximum number of attempts exceeded");
}
}
Solution 1:[1]
Whenever, the executeLogin() will be invoked, the previous value of totalAttempts will be erased and it will be again initialized to 3. To control this, you can make the totalAttempts as global
int totalAttempts= 3;
private void executeLogin() {
String userNameStr = userNameTF.getText();
String passWordStr = passWordTF.getText();
if (totalAttempts != 0) {
if (userNameStr == "temp" && passWordStr == "pass") {
System.out.println("Correct");
else {
System.out.println("Incorrect");
totalAttempts--;
} else {
System.out.println("Maximum number of attempts exceeded");
}
}
Or if you are declaring it inside the class make it static.
Solution 2:[2]
So there are 3 main problems with your code:
You use while loop - though you shouldn't loop at all, function should be called every time login button is pressed.
Number of attempts cannot be local variable - you should keep its value for future use (so global variable then)
You are comparing string the wrong way (not
==butequals)public class MyForm extends ... { int totalAttempts = 3; private void login() { String userNameStr = userNameTF.getText(); String passWordStr = passWordTF.getText(); if (totalAttempts != 0) if ("temp".equals(userNameStr) && "pass".equals(passWordStr)) System.out.println("Correct"); else { System.out.println("Incorrect"); totalAttempts--; } else System.out.println("Maximum number of attempts exceeded"); } }
Solution 3:[3]
Your problem is that the while loop doesn't wait for the user to press the button again. Assuming that executeLogin() is called every time the button is pressed, you need to keep a global attempts variable and decrement it every time the method is called rather than multiple times within the same call.
int totalAttempts = 3;
private void executeLogin() {
String userNameStr = userNameTF.getText();
String passWordStr = passWordTF.getText();
if (totalAttempts != 0) {
if (userNameStr == "temp" && passWordStr == "pass") {
System.out.println("Correct");
else {
System.out.println("Incorrect");
totalAttempts--;
} else {
System.out.println("Maximum number of attempts exceeded");
}
}
Solution 4:[4]
Your code just performs the same login totalAttempts times. You don't cede control. In event-driven programming, you do not wait for the user to do something, you set it up and do things in response to a user doing something. In other words, you never write a code that actively waits for a user to input the password again.
The main thing is, there is no such thing as "limiting the number of logins to 3". You can limit the number of consecutive incorrect logins to 3, you can limit the number of consecutive incorrect user logins to 3, you can limit the number of incorrect logins per period to 3 - all of those require some data structure to hold the information about the failed logins.
Solution 5:[5]
The problem in your code is that you don't request the user to enter his username and password again. My fix would be:
Design the function so that it requests the credentials in the loop
private void executeLogin() {
String userNameStr;
String passWordStr;
int totalAttempts = 3;
while (totalAttempts != 0) {
userNameStr = userNameTF.getText();
passWordStr = passWordTF.getText();
if (userNameStr == "temp" && passWordStr == "pass") {
System.out.println("Login Correct!");
return;
} else {
System.out.println("Incorrect Login");
totalAttempts--;
System.out.println(totalAttempts);
}
}
if (totalAttempts == 0) {
System.out.println("Maximum number of attempts exceeded");
}
}
Solution 6:[6]
package javaconcepts;
import java.util.Scanner;
public class JavaLoginPrg {
private void login() {
String username, password;
int totalAttempts = 3;
while (totalAttempts != 0) {
if (username == "temp" && password == "pass") {
System.out.println("Login succeeded!");
return;
} else {
System.out.println("Login failed!");
totalAttempts--;
System.out.println(totalAttempts);
}
}
}
public static void main(String[] args) {
String temp, pass;
Scanner sc = new Scanner(System.in);
System.out.println("Enter username");
username = sc.toString();
System.out.println("Enter password");
password = sc.toString();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prakash |
| Solution 2 | PKey |
| Solution 3 | elaid |
| Solution 4 | Piotr Wilkin |
| Solution 5 | Kostas Pelelis |
| Solution 6 | Ashok kumar Ganesan |
