1

Our teacher gave us a new task and somehow I am not able to figure out the problem.

There are 3 different java classes and I am only allowed to change the ToDoList class. There, I want to add a new List so that the main class is able to add new Items to my todo list. As you can see below, I tried to initialize a new list but that did not work.

Where is my mistake?

public class ToDoListEntry {
   String task;
   LocalDate date;
   ToDoListEntry next;

   public ToDoListEntry(LocalDate date, String task) {
      this.task = task;
      this.date = date;
   }
}

Then comes the next where I tried to add an array but which did not work:

public class ToDoList {
   ToDoListEntry first;

   public ArrayList<ToDoListEntry> todolist;

   public ToDoList (){
      todolist = new ArrayList<ToDoListEntry>();
   }

   public void add(ToDoListEntry newTask) {
      todolist.add(newTask);
  }

  public String print() {
    String result = "";
    if (first == null) {
        result = "Empty list!\n";
    } else {
        ToDoListEntry pointer = first;
        while (pointer != null) {
            result += "Until " + pointer.date + " Task: "
                    + pointer.task +"\n";
            pointer = pointer.next;
        }
    }
    System.out.println(result);
    return result;
}
}

And in the end, the main class should supposed to create a new ToDo List and print it out (Note that I did not include the print() Method):

public static void main(String[] args) {

    System.out.println("Test 00: Empty List");
    ToDoList list2016 = new ToDoList();

    list2016.print(); 

    System.out.println("Test 01: add");
    list2016.add(new ToDoListEntry(LocalDate.of(2016, 8, 15), "Do workout"));
    list2016.add(new ToDoListEntry(LocalDate.of(2016, 6, 3), "Buy apples"));
    list2016.add(new ToDoListEntry(LocalDate.of(2016, 10, 11), "Read Books"));
    list2016.print();
10
  • Did you import it? Please post the error. Commented Dec 13, 2015 at 11:40
  • Could you add the error that you mention add to your post? Commented Dec 13, 2015 at 11:41
  • Define "doesn't work". Precisely. What is the error message you get? Also, given that the ToDoListEntry has a field named next, of type ToDoListEntry, I think you're not supposed to use an ArrayList, but to chain entries to each other (i.e. understand the principle of a linked list). Commented Dec 13, 2015 at 11:41
  • Okay sorry for calling it an error! So far I did not get any errors but the thing is that all the three todos are not added to my ToDo list. And for that I don't understand why these could not be added. Commented Dec 13, 2015 at 11:42
  • 1
    @Tom you're totally right ;) Commented Dec 13, 2015 at 11:53

1 Answer 1

4

When you add a new entry to the list, you don't set the next pointer of that entry. But in the print() method, you use the next pointer, but (if you don't set it somewhere else) it is still null. Try your add() method like this:

public void add(ToDoListEntry newTask) {
    todolist.add(newTask);
    if (todolist.size() >= 2) todolist.get(todolist.size()-2).next = newTask;
}

But are you sure you can use an ArrayList here? I get the impression that you have to implement a linked list. In this case the code would look like this:

class ToDoListEntry {
    String task;
    LocalDate date;
    ToDoListEntry next;

    public ToDoListEntry(LocalDate date, String task) {
       this.task = task;
       this.date = date;
    }
}

public class ToDoList {
    ToDoListEntry first;
    int size;

    public ToDoList (){
        first = null;
        size = 0;
    }

    public void add(ToDoListEntry newTask) {
        if (first == null){
            first = newTask;
        }else{
            ToDoListEntry pointer = first;
            while (pointer.next != null){
                pointer = pointer.next;
            }
            pointer.next = newTask;
        }
        size++;
    }

    public String print() {
        String result = "";
        if (first == null) {
            result = "Empty list!\n";
        } else {
            ToDoListEntry pointer = first;
            while (pointer != null) {
                result += "Until " + pointer.date + " Task: " + pointer.task +"\n";
                pointer = pointer.next;
            }
        }
        System.out.println(result);
        return result;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

that really helped! Thanks for that! Now I just have one question left: How do I sort the list entries by its date? So that the first entry is the next due todo?
@Yanlu you can do this by calling Collections.sort( list ); and implementing Comparable<TodoListEntry>

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.