2

starting out with Java and have run into some issues. I have created a class called: Admin, which a user will give attributes to such as a name and other pedigree info...but the main function of the Admin class is to create arrays that will be used as a queue system.

I am trying to allow the Admin to create as many arrays as they desire. However I am having trouble figuring out a way to differentiate between each of the arrays when it comes to reiterating them back to the user. For instance, lets say an "Admin" was a Bank and wanted to create an array that a "User"(i.e. customer, which has it's own class) could join to get in line to see a teller. The Bank may also want to create a line for a "User" to see a Loan Officer, etc.

I am not able to allow the Admin to give each array a specific reference variable that would differentiate it from others by doing this:

System.out.println("Enter the name of the line you wish to create: ");
String lineName=keyboard.nextLine();
ArrayList<User>lineName=new ArraryList();`

The program gives an error saying that the variable has already been initialized in the method when I do so, which I kind of understand. However having the functionality of knowing what line I am looking at within the code is invaluable to me. Another reason I wish to do this is because I want to create an Array of Arrays that would show a customer all of the "lines" they could potentially join. So I would like the output to look something like this:

John Hancock Bank lines:

Teller Window

Loan Officer

Mortgage Specialist

etc.

From there I would allow the user to access the element of the array they wish to join.

What would be the best way to "identify" each specific array that is created by an Admin?

4
  • 2
    You could "name" the arrays, and keep them in a Map. You can then retrieve from the map by name. E.g., Map<String, List<User>. However, are you certain the a List is appropriate? I might think a queue or stack structure would better model a line of people. Commented May 10, 2016 at 17:24
  • 1
    you may want to use a docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html to model your Queue rather than a list. Commented May 10, 2016 at 17:32
  • As I am doing more coding in Java I am discovering all of these useful tools! I am taking a class on Java Programming and we haven't learned about queue or stack structure's yet. Thank you for your input! I am excited to learn all of these new tools. Commented May 10, 2016 at 17:36
  • It's great you're excited :) Commented May 10, 2016 at 17:46

3 Answers 3

3

Typically, variable names have meaning to a programmer, and not to a user. If you want to associate a list with a name, you'd either want to use a Map like so:

Map<String, ArrayList<String> lines = new HashMap<String, ArrayList<String>>();
lines.put(keyboard.nextLine(), new ArrayList<String>());

Or create a new class to model this association:

class Line{
   String lineName;
   ArrayList<String> people;

   public User(String name, ArrayList<String> people){
     this.lineName = name;
     this.people = people;
   }
}

The comments are above are right though. I think a Queue would be better for your purpose.

Sign up to request clarification or add additional context in comments.

Comments

2

You could use a HashMap.

Map<String, List<User>> map = new HashMap<String, List<User>>();

System.out.println("Enter the name of the line you wish to create: ");
map.put(keyboard.nextLine(), new ArraryList<User>());

Comments

0

The error about the you're getting about the variable that has already been initialized is due to you reusing a variable name as follows:

String lineName=keyboard.nextLine();
ArrayList<User> lineName=new ArraryList();`

You have created two variables but they have the same name. Each variable must have an unique name - otherwise when you later refer to lineName there's no way to know if you mean the String or the ArrayList.

As other have noted, in general when you want stuff like a "line" that has a "line name" and a value, the correct data type to store this information is a map. A map consists of key-value pairs, where the key is the "line name" and the value is the actual data.

You'll notice people putting a lot of emphasis in choosing the correct data type and structure for storing your information. This is a very core concept in programming: choosing the correct data structure and type to use is extremely important in producing effective and good code and it's best to grasp these concepts well from the get-go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.