Question

In: Computer Science

Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...

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 )

Solutions

Expert Solution

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


Related Solutions

Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
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...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for a number (it must work if user enters numbers with decimal point of not). Then your program must print number is negative -- if humber is negative number in [0,10) -- if it is between 0 and 10, not including the 10 number in [10,100) -- if it is between 10 and 100, not including the 100 number in [100,1000) -- if it is...
TwoNumbersAddTo 1. write a class called TwoNumbersAddTo with a main method 2. the program asks for...
TwoNumbersAddTo 1. write a class called TwoNumbersAddTo with a main method 2. the program asks for a number (lets call it "goal"). 3. then the program reads at most 20 integers (i.e. any number of integers from 0 to 20) 4. the program stops reading when user enters 0 or user has entered 20 numbers. 5. program prints all the pairs that add up to goal. 6. Notice that if goal is 5, for example, 1 + 4 and 4...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
Write a public class call CountInts. class CountInts has a main method. CountInts must have two...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two methods: count and displayResults. 1. method count takes an array, aa, of ints and counts how many times each integer appears in aa. The integers can only be from 0 to 100 inclusive. 2. method display results displays the results of the count 3. use the template provided. insert your code in the places indicated and don't change anything else. Examples % java CountInts...
Part I: Have a program class named TestArrays This class should have a main() method that...
Part I: Have a program class named TestArrays This class should have a main() method that behaves as follows: Have an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method)....
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method). It will be in the default package. It will import edu.wiu.StdDraw. Its main method will make calls to methods in StdDraw to draw something iconic about your hometown. Multiple colors should be used.Multiple primitive types will be used and the drawing will consists of more than 20 primitive shapes.
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT