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 JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
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...
This class contains the main method. In this class you should: Ask the user for the...
This class contains the main method. In this class you should: Ask the user for the name, job tilte, and salary. Instantiate a Employee object with those parameters. Use the accessor methods to check the value of all three instance variables and print them. Ask the user to enter a percentage to raise the salary by. Use the raise method to increase the salary. Use the salary accessor check the new value of salary and print it.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT