user103210
user103210

Reputation: 13

Arraylist output is not coming out as intended

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;  
}   

Upvotes: 0

Views: 222

Answers (6)

rupesh jain
rupesh jain

Reputation: 3430

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

Upvotes: 0

dimcookies
dimcookies

Reputation: 1930

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

class ToDo {
...
   @Override
   public String toString() {
    return this.priority + "," + ...;
   }
...
}

Upvotes: 0

SparkOn
SparkOn

Reputation: 8946

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());
}

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

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

}

Upvotes: 1

Duncan Jones
Duncan Jones

Reputation: 69329

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.

Upvotes: 0

August
August

Reputation: 12558

You need to override toString() in ToDo

For example:

@Override
public String toString() {
    return detail + "," + importance + "," + priority + "," + urgency;
}

Upvotes: 0

Related Questions