0

Possible Duplicate:
Java Object default toString

Why is it when I print my List from filereader it is [myServiceOrder@3bc1cac, myServiceOrder@32fe621e, myServiceOrder@5adbb9b9, myServiceOrder@f7e4f49, myServiceOrder@2d874991, myServiceOrder@ceee5f1, myServiceOrder@183a37d9]

public class myServiceOrder implements ServiceOrder, Comparable<myServiceOrder>{

private int number=0;
private String ownerName="";
private String make="";
private String model="";
private int year=0;

public myServiceOrder(int number, String ownerName, String make, String model,  int year) {
    this.number=number;
    this.ownerName=ownerName;
    this.make=make;
    this.model=model;
    this.year=year;
}

public myServiceOrder() {
    // TODO Auto-generated constructor stub
}

@Override
public void setOrderNum(int orderNumber) {
    number=orderNumber;
}

@Override
public void setYear(int year) {
    this.year=year;
}

@Override
public void setOwner(String ownerName) {
    this.ownerName=ownerName;
}

@Override
public void setMake(String make) {
    this.make=make;
}

@Override
public void setModel(String model) {
    this.model=model;

}

@Override
public String getOwner() {
    return ownerName;
}

@Override
public String getMake() {
    return make;
}

@Override
public String getModel() {
    // TODO Auto-generated method stub
    return  model;
}

@Override
public int getOrderNum() {
    // TODO Auto-generated method stub
    return number;
}

@Override
public int getYear() {
    // TODO Auto-generated method stub
    return year;
}

@Override
public String getMakeModelYear() {
    // TODO Auto-generated method stub
    return make+ " "+ model+ " "+ year+ " ";
}

@Override
public boolean equals(ServiceOrder otherServiceOrder) {
        if (getOrderNum()==otherServiceOrder.getOrderNum())
            return true;
        else
    return false;
}

@Override
public int compareTo(ServiceOrder otherServiceOrder, int key) {
    int comparisonResult=0;
    if(key==1)
    {
        if(getOrderNum()< otherServiceOrder.getOrderNum())
            comparisonResult= -1;
        if(getOrderNum()== otherServiceOrder.getOrderNum())
            comparisonResult= 0;
        if(getOrderNum()> otherServiceOrder.getOrderNum())
            comparisonResult= 1;
    }
        else if(key==2)
        {
            comparisonResult = getOwner().compareTo(otherServiceOrder.getOwner());                      
        }

        else if(key==3)
        {
         comparisonResult = getOwner().compareTo(otherServiceOrder.getOwner());
        }
    return comparisonResult;

}

@Override
public int compareTo(myServiceOrder arg0) {
    // TODO Auto-generated method stub
    return 0;
}

}


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import javax.swing.JOptionPane;


public class List extends LinkedList<myServiceOrder> {
private static  LinkedList<myServiceOrder> newList = new LinkedList();

 public  void  Print() throws Exception
 {
     System.out.println(newList);
 }
public   LinkedList<myServiceOrder> createServiceOrder(File inFile) throws Exception {
    int number=0;
     String ownerName="";
     String make="";
     String model="";
     int year=0;
     myServiceOrder serviceList = new myServiceOrder();    
        Scanner fileScan=new Scanner(inFile);
            while (fileScan.hasNext())
            {
                String ignore;

                number = fileScan.nextInt();
                //System.out.println(number);
                ignore = fileScan.nextLine(); // ignore the newline
                ownerName = fileScan.nextLine();
            //  System.out.println(ownerName);
                make = fileScan.nextLine();
        //      System.out.println(make);
                model = fileScan.nextLine();
            //  System.out.println(model);
                year = fileScan.nextInt();
        //      System.out.println(year);
                ignore = fileScan.nextLine(); // ignore the newline
                  serviceList = new myServiceOrder( number,  ownerName,  make,        model,  year);         
                  newList.add(serviceList);

            }
        fileScan.close();


     //   System.out.println(newList.viewAll());
        return newList; 
    }


}

Ok I see, my was I dense. I also have a second question: I have to sort the list three different ways depending in my GUI what option I select, I assume that I implement Comparable, but in my compareTo interface it is compareTo(Object o, int key). How can I use that key if the sort method is just Object o. Should I try using a Comparator? if my key=1 how can I tell it to sort that way in my List class?

Community
  • 1
  • 1
  • 3
    The gibberish is the `hashcode` of the object. To get more friendly printouts, you need to override the `toString()` method in your class. – DNA Sep 29 '12 at 22:44
  • It would have been better to pay closer attention to formatting so that the second question wasn't hidden by the code formatting =_= – Meredith Jul 01 '13 at 00:21

4 Answers4

7

Classic case of a missing override of the toString() method in your myServiceOrder class.

Take a look here for examples in implementation. This page and Rohit's answer give explanations as to why you need to override toString().




Argh didn't see your second question until now when it's very late:

See this question and this question on the differences between using the Comparable interface vs using the Comparator interface.

Community
  • 1
  • 1
Meredith
  • 3,928
  • 4
  • 33
  • 58
3

How would Java know how you want myService objects to be printed? You can tell it by overriding toString:

@Override
public String toString() {
    return "myServiceObject#" + number + "[" + ownername + ", " + make + ", " + model + ", " + year + "]";
}
meriton
  • 68,356
  • 14
  • 108
  • 175
1

What gets printed is actually the hashcode of the object you print without overriding toString method.. Now since you're printing LinkedList, you can't do that.. Rather you can iterate over the list and print individual element: -

public  void  Print() throws Exception
 {
    for (myServiceOrder so: newList) {
         System.out.println(so)
    }
 }

Now, since serviceOrder is itself an object.. You would need to override your toString() in that class..

@Override
public String toString() {
    return this.ownerName + this.make + "[" +  this.model + " - " + String.valueOf(this.year) + "]";            
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 1
    Printing a `LinkedList` object automatically iterates over the objects in the list. In fact, this happens in the `LinkedList.toString()` method. – Code-Apprentice Sep 29 '12 at 22:50
  • Thanks Code-Guru for the clarification – Meredith Sep 29 '12 at 22:52
  • @Code-Guru.. Yeah that is true.. I just used this way to print without that square bracket at the end `[`, and `]`.. And I don't think this deserves a down-vote.. – Rohit Jain Sep 29 '12 at 22:57
  • I'm still a little confused, in myServiceOrder class what would my toString look like and then how could I print that – user1672282 Sep 29 '12 at 23:19
  • You can add any variable to your return value, that you want to print, and format it in whatever way you want.. That is why `toString()` is overrided.. – Rohit Jain Sep 29 '12 at 23:21
  • `toString()` method is automatically invoked when you print an instance of that class.. And the string returned from `toString()` is printed.. – Rohit Jain Sep 29 '12 at 23:24
  • Ok I see, my was I dense. I also got a second question, I have to sort the list 3 differant ways depending in my GUI what option I select, I assume that I implement Comparable, but in my comparto inter face it is compareTo(Object o, int key) how can I use that key if the sort method is just Object o. Should I try comparator? – user1672282 Sep 29 '12 at 23:45
1
System.out.println(newList);

This automatically calls the toString() method of the LinkedList class which in turn calls toString() on each of the references in the list (your ServiceOrder objects, in this case). Since you have not provided your own toString() method, the default one in Object is used. This gives the funny output myServiceOrder@3bc1cac which is Java's default way of printing a reference variable. If you wish to see something else, you need to tell Java how to do this by implementing toString() in your ServiceOrder class.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268