Hashmaps password lock java -
i have made password lock thing in java test skills in learning hash maps. code:
package com.zach.password; import java.util.hashmap; import java.util.random; import java.util.scanner; public class password { public static void main(string[] args) { @suppresswarnings("resource") scanner in = new scanner(system.in); random r = new random(); hashmap<string, string> names = new hashmap<string, string>(); hashmap<string, integer> passwords = new hashmap<string, integer>(); hashmap<string, integer> randnums = new hashmap<string, integer>(); while(true) { system.out.println("please enter name!!"); string name = in.nextline(); if(names.containskey(name)) { int tries = 2; while(tries != 0) { if(tries != 0) { system.out.println("what password?"); int normpassword = in.nextint(); if(normpassword == passwords.get(name)) { system.out.println("welcome, " + name); system.out.println("this random number:\n"); break; } else system.out.println("incorrect"); } else if(tries == 0) { system.out.println("this account locked! unlock entering random number!!"); int lockedrand = in.nextint(); if(lockedrand == randnums.get(name)) { system.out.println("welcome, " + name); system.out.println("this random number:\n"); break; } else system.out.println("incorrect"); } } } else { int randnum = 1 + r.nextint(99); randnums.put(name, randnum); names.put(name, name); system.out.println("enter password!!"); int password = in.nextint(); passwords.put(name, password); system.out.println("welcome, " + name); system.out.println("this random number:\n" + randnum); } } }
}
i not sure problem is, whenever enter name goes else statement if names contains key... not stop program , rerun... doesn't go down there.
you have use replace in.nextline()
in.next();
scanning name. in.next();
blocks application until user input submitted. in.nextline()
gives rest of current line , changes position. therefore returns empty string if no input given user.
Comments
Post a Comment