In: Computer Science
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a user name: liam welcome back: liam Here is the list of users: Liam Noah William James Oliver Benjamin admin Elijah Lucas Mason Logan Alexander admin Ethan Jacob Michael Daniel admin Henry Jackson Sebastian Don't use switch
Examples:
emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a user name: liam welcome back: liam emacs% java CheckUserName enter a user name: Oliver welcome back: Oliver emacs% java CheckUserName enter a user name: Benjamin welcome back: Benjamin you have admin privileges emacs% java CheckUserName enter a user name: benjamin welcome back: benjamin you have admin privileges emacs% java CheckUserName enter a user name: foo You are not a recognized user emacs%
I have this for the java code
import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; public class CheckUserName { public static void main(String[] args) { //users list HashSet<String> users = new HashSet<>(); users.add("liam"); users.add("noah"); users.add("william"); users.add("james"); users.add("oliver"); users.add("elijah"); users.add("lucas"); users.add("mason"); users.add("jacob"); users.add("logan"); users.add("ethan"); users.add("michael"); users.add("henry"); users.add("sebastian"); users.add("jackson"); //admins list HashSet<String> admins = new HashSet<>(); admins.add("benjamin"); admins.add("alexander"); admins.add("daniel"); Scanner sc = new Scanner(System.in); System.out.println("enter a user name: "); String name = sc.nextLine(); //checking in users list if (users.contains(name.toLowerCase())) { System.out.println("Welcome back " + name); } //checking in admins list else if (admins.contains(name.toLowerCase())) { System.out.println("Welcome back " + name); System.out.println("you have admin privileges "); } else System.out.println("You are not a recognized user"); } }
but I run it, it says "enter a user name:" your output, "enter a user name: welcome back: sebastian" expected output (error with the different names )
import java.util.HashSet;
import java.util.Scanner;
public class CheckUserName {
public static void main(String[] args) {
// users list
HashSet<String> users = new HashSet<>();
users.add("liam");
users.add("noah");
users.add("william");
users.add("james");
users.add("oliver");
users.add("elijah");
users.add("lucas");
users.add("mason");
users.add("jacob");
users.add("logan");
users.add("ethan");
users.add("michael");
users.add("henry");
users.add("sebastian");
users.add("jackson");
// admins list
HashSet<String> admins = new HashSet<>();
admins.add("benjamin");
admins.add("alexander");
admins.add("daniel");
Scanner sc = new Scanner(System.in);
// need to use print instead of println
System.out.print("enter a user name: ");
String name = sc.nextLine();
// checking in users list
if (users.contains(name.toLowerCase())) {
System.out.println("Welcome back " + name);
}
// checking in admins list
else if (admins.contains(name.toLowerCase())) {
System.out.println("Welcome back " + name);
System.out.println("you have admin privileges ");
}
else
System.out.println("You are not a recognized user");
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me