Linus Lindvall
Linus Lindvall

Reputation: 29

Accessing variable of ArrayList inside class

The class Task is describing tasks for a business which includes the variables: date, description, total amount of hours the task will take to complete, and the owner of the task.

An ArrayList is created for all the tasks named tasks. The problem is that a task can have more than one owner, meaning that creating a variable called owner wont work, so what I've done is that I created another class called TaskOwner and implemented that class as an ArrayList named taskOwner inside the Task class.

Now to the problem: You are supposed to be able to list tasks by a specific owner: meaning that you need to compare owners to the name you enter on your keyboard.

The problem in this lies within these lines of code:

System.out.println("What name of owner do you want to list tasks for: ");
String nameOfOwner = keyboard.nextLine();

if (nameOfOwner.toLowerCase().equals(tasks.get(1).getTaskOwner().getName().toLowerCase())) {
    System.out.println(tasks.get(1));
}

I can't seem to access the variable named name inside the class TaskOwner, even though I've created getters for everything that is needed, so does anyone know how I am supposed to be able to access this information?

The error message I get is:

The method getName() is undefined for the type ArrayList

Upvotes: 2

Views: 766

Answers (3)

n3o
n3o

Reputation: 2873

As pointed out in other answers, the problem is you are calling getName() on an ArrayList<> instead of an object inside the ArrayList. The correct way of doing this would be to loop over all the tasks and then for each task, loop over their owners. Here is a sample piece of code, assuming the owners name is stored in the variable nameOfOwner:

for(Task task: tasks) {
  for(TaskOwner owner: tasks.getTaskOwner) {
     if (nameOfOwner.toLowerCase().equals(owner.getName().toLowerCase())) {
         System.out.println(task);
         break;
     }
  }
}

If you have also overloaded the equals method in the class TaskOwner to do a string match for the owner's name, you could just use the Arraylist.contains() method. But then, you will need to create a TaskOwner object out of the user input.

If your intention is to do task and owner lookups, you should also consider using HashMap. This would give you a better performance than ArrayList<> for direct lookups.

Upvotes: 0

M A
M A

Reputation: 72844

getTaskOwner appears to return an object of type ArrayList and not TaskOwner (that's what the error message indicates). In other words, it returns a list of owners. To call the getName() method, you need to loop over this list, and call the method on each element corresponding to an instance of TaskOwner.

System.out.println("What name of owner do you want to list tasks for: ");
String nameOfOwner = keyboard.nextLine();

for(int i = 0; i < tasks.size(); i++) {
    List<TaskOwner> owners = tasks.get(i).getTaskOwner();
    for(TaskOwner owner : owners) {
       if (nameOfOwner.toLowerCase().equals(owner.getName().toLowerCase())) {
           System.out.println(tasks.get(i));
           break;
       }
    }
}

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

This returns an ArrayList:

tasks.get(1).getTaskOwner();

So you will need to call array list methods on it such as contains(...)

if (tasks.get(1).getTaskOwner().contains(nameOfOwner.toLowerCase())) {

Upvotes: 0

Related Questions