Reputation: 868
I created a small snippet to illustrate my issue. The first check is able to determine that the user already exists if I were to input one of those usernames. But the second input is able to bypass the check. How can I do it to make sure no one can bypass it?
public static void main(String[] args) {
HashMap<String, String> uID = new HashMap<>();
Scanner scan = new Scanner(System.in);
String input = null;
uID.put("James", "25");
uID.put("Jack", "25");
uID.put("John", "25");
System.out.println("Enter your username and age");
input = scan.nextLine();
if (uID.containsKey(input)) {
System.out.println("Username already exist, choose another username.");
input = scan.nextLine();
uID.put(input, "25");
}
}
Upvotes: 0
Views: 1317
Reputation: 71
"If" is a single time condition check and executes the statements in it if the condition satisfies, that is why after your first check, the second input surpasses it. You can better use "while" for a continuous checking process everytime and executing statements.
Upvotes: 0
Reputation: 688
You need to split username and age before checking against the map and storing to map.
public static void main(String[] args) {
HashMap<String, String> uID = new HashMap<>();
Scanner scan = new Scanner(System.in);
String input = null;
uID.put("James", "25");
uID.put("Jack", "25");
uID.put("John", "25");
//prompt user to input details
System.out.println("Enter your username and age");
input = scan.nextLine();
//split name and age
String arr[]=input.split("\\s");
//check if name is already present in the map
while (uID.containsKey(arr[0])) {
//if already present,prompt user to reenter username alone
System.out.println("Username already exist, choose another username.");
//store the username to array's 0th index(as age is already present in 1st index)
arr[0] = scan.nextLine();
}
//if details not present,then store it to the map
uID.put(arr[0], arr[1]);
System.out.println("Your details saved..");
System.out.println("uID="+uID);
}
Upvotes: 0
Reputation: 4490
Alternatively, you may use a do-while
loop.
do
{
System.out.println("Enter unique username and age");
input = scan.nextLine();
}while(uID.containsKey(input));
uID.put(input, "25");
Here we're asking for username, until we get a non-existing user name.
Upvotes: 0
Reputation: 18233
An if
statement checks a single condition, and then runs the code inside the code block if that condition is met. A while
statement is the next level of that. It'll check the condition—in your case, whether or not the key is in the map—and then run the code inside if the condition is true. After running the code it checks the condition again, and will keep doing it over and over until it stops being true. That is, it runs the code inside that block while the condition is true—while their input is already in the map.
Use a while
loop to check if the key is invalid, and force the user to keep entering values until they enter a valid one. Don't update the map value until after that's been validated:
public static void main(String[] args) {
Map<String, String> uID = new HashMap<>();
Scanner scan = new Scanner(System.in);
String input = null;
uID.put("James", "25");
uID.put("Jack", "25");
uID.put("John", "25");
System.out.println("Enter your username and age");
input = scan.nextLine();
while (uID.containsKey(input)) {
System.out.println("Username already exist, choose another username.");
input = scan.nextLine();
}
uID.put(input, "25");
}
Upvotes: 3