0

Forgive my formatting, I'm new to coding and these boards. I'm trying to make a simple todo list as practice in java. It reads and parses data from a text file, then sorts and prints it.

my output looks like this: [ToDoList003_002.ToDo@4cc7014c] output should be something like: [get milk,important,highpriority,urgent]

package ToDoList003_002;


import java.util.*;
import java.io.*;


public class ToDoList002 {
ArrayList<ToDo> toDoList=new ArrayList<ToDo>();

public static void main(String[] args) {

    new ToDoList002().go();
}//close main

    public void go(){
        getItems();
        Collections.sort(toDoList); //002
        System.out.println(toDoList);

    }

    void getItems(){
        try{
            File file=new File("/Users/lew/Dropbox/JAVA/CodePractice/src/ToDoList003_002/todolist.txt");
            BufferedReader reader = new BufferedReader (new FileReader(file));
            String line=null;
                while ((line=reader.readLine()) !=null){
                    addItem(line);
                }
        }catch(Exception ex){
                    ex.printStackTrace();
                }



    }

    void addItem(String lineToParse){
        String[] tokens=lineToParse.split("/");
        //toDoList.add(tokens[0]);
        //toDoList.add(tokens[1]);
        ToDo nextTodo= new ToDo(tokens[0], tokens[1],tokens[2],tokens[3]);
        toDoList.add(nextTodo);
    }



    //private static void add(String string) {
        // TODO Auto-generated method stub

    }





package ToDoList003_002;

import java.util.ArrayList;

public class ToDo implements Comparable<ToDo>{
String detail;
String importance;
String priority;
String urgency;

public int compareTo (ToDo d){
    return detail.compareTo(d.getDetail());
}

ToDo(String d, String i, String p, String u){
    detail=d;
    importance=i;
    priority=p;
    urgency=u;
    //set variables in constructor
}
public String getDetail(){
    return detail;
}

public String getImportance(){
    return importance;
}

public String getPriority(){
    return priority;
}

public String getUrgency(){
    return urgency;

public String toString(){
    return detail;  
}   
user103210
  • 13
  • 1
  • http://stackoverflow.com/questions/9265719/print-arraylist – 001 Aug 13 '14 at 14:59
  • The code does appear to contain a `toString()` method in class `ToDo`, but cannot be a copy-paste of the actual code because of syntax errors. – Patricia Shanahan Aug 13 '14 at 15:02
  • i've tried the 6 answers so far, and the ones the ones that compile still give me hashes. I notice I am getting some errors on the toString() in eclipse: multiple markers at this line.....syntax error, insert enumbodyto complete block statement.... syntax error token "String" @ expected... syntax error, insert "enum identifier" to complete enumHeaderName – user103210 Aug 13 '14 at 16:08
  • ok i really looked through it and found a stray bracket which was affecting my tostring. The override helped once that was fixed thanks! – user103210 Aug 13 '14 at 18:13

6 Answers6

1

You can use a for-each loop to print the contents of your list like this : Note : you have to override toString() of your ToDo class and use this

   public static void main(String[] args) {
    List<String> ls = new ArrayList<String>(); // use ToDo instead of String here
    ls.add("a");
    ls.add("b");
    ls.add("c");
    for (String s : ls) {
        System.out.println(s);
    }
}

O/P

a
b
c

override toString() of Todo class like this :

@Override
public String toString() {
    return detail + "," + importance ; // add other fields if you want

}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

ToDo class should override the toString method to provide a string representation of the object

class ToDo {
...
   @Override
   public String toString() {
    return this.priority + "," + ...;
   }
...
}
dimcookies
  • 1,930
  • 7
  • 31
  • 37
0

You need to override toString() in ToDo

For example:

@Override
public String toString() {
    return detail + "," + importance + "," + priority + "," + urgency;
}
August
  • 12,410
  • 3
  • 35
  • 51
0

When you call System.out.println(someObject);, you invoke the toString() method on that object. An ArrayList doesn't override the standard toString() method inherited from Object, so the result is Type name + @ + hashcode.

You need to override toString() in your ToDo list so that it prints prettily. Then adopt an approach to print your collection, e.g.

System.out.println(Arrays.toString(toDoList.toArray()));

See Printing Java collections nicely for further inspiration.

Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
0

This line tells that you are adding ToDo object to the list

toDoList.add(nextTodo);

Then you are sorting the object and trying to print them.

Collections.sort(toDoList);
System.out.println(toDoList);

So first get the Todo object out of the list then try to get the values of the object Something like this

for(ToDo todo : toDoList)
{
  System.out.println(todo.getDetail()+"\t"+
                   todo.getImportance()+"\t"+
                   todo.getPriority()+"\t"+
                   todo.getUrgency());
}
SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

You need to override toString methods.From the oracle docs:

public String toString()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Returns: a string representation of the object.

Sample toString() implementation:

public final class Vehicle {

  private String fName = "Dodge-Mercedes"

  @Override public String toString() {
    StringBuilder result = new StringBuilder();
    String NEW_LINE = System.getProperty("line.separator");
    result.append(this.getClass().getName() + " Object {" + NEW_LINE);
    result.append(" Name: " + fName + NEW_LINE);
    result.append("}");
    return result.toString();
  }
}  

If you are using eclipse you can use Alt+Shift+S+S to override toString() method automatically for a class..This shortcut might not be useful in production code(it prints out all the field name,which could be not good from security perspective) but for development purposes it will suffice

rupesh jain
  • 3,410
  • 1
  • 14
  • 22