2

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

1
  • 1
    Why have a class called TaskOwner when you can simply place an ArrayList<Owner> variable inside of Task? Commented Dec 26, 2014 at 14:49

3 Answers 3

2

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;
       }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes this solved it thank you! I understood the problem in this that it returned the ArrayList and not the object itself, just didnt know how to solve it :)
0

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())) {

Comments

0

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.

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.