2

so I'm trying to figure out how to print the actual contents, not memory locations, of my array list

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class hw2redo 
{
    public static void main(String args[]) throws FileNotFoundException
    {
         //Scan file for data
         GeometricObject g = null;
         BufferedReader file = new BufferedReader(new FileReader("file.txt"));
            Scanner diskScanner = new Scanner(file);
            //Create dynamic array list
            ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
            //Scan data and add data to list
            while(diskScanner.hasNext())
            {
                String geolist = diskScanner.nextLine();
                g = recreateObject(geolist);

                list.add(g);

            }

            showObjects(list);

    }
    private static GeometricObject recreateObject(String data)
    {
        GeometricObject object = new GeometricObject(data);
        return object;
    }
    private static void showObjects(ArrayList<GeometricObject> list)
    {
    for(GeometricObject o : list)
      System.out.println(o);
    }


}
class GeometricObject
{

    public GeometricObject(String data) {
        // TODO Auto-generated constructor stub
    }

}

So here is my code. I have tried using the toString() and Arrays.toString() but they dont seem applicable for an arraylist (I tried because they worked on my regular arrays).

The output I'm recieving is

// Output
GeometricObject@55f96302
GeometricObject@3d4eac69
GeometricObject@42a57993
GeometricObject@75b84c92
GeometricObject@6bc7c054
GeometricObject@232204a1

which is good because I'm close, I just need to figure out how to print the actual contents.

The content I'm looking for in my file.txt is

Circle,green,false,4.0
Circle,blue,false,2.0
Circle,blue,true,7.0
Rectangle,orange,true,10.0,6.0
Rectangle,green,false,5.0,11.0
Rectangle,red,true,14.0,12.0

Any help would be much appreciated. Thanks!

RiFF RAFF
  • 131
  • 2
  • 11
  • You need to override `GeometricObject#toString()` – Mureinik Oct 11 '15 at 06:08
  • Could you elaborate a little further? I know override means to provide the exact same argument list of the method, but what would i provide inside the method? @Eran – RiFF RAFF Oct 11 '15 at 06:09
  • @RiFFRAFF You need not to change the current method. Keep the code as it is and just ovveride the toString method in GeometricObject. That's it. – Suresh Atta Oct 11 '15 at 06:14

2 Answers2

5

You need a toString method in your class:

class GeometricObject
{

    private String data;

    public GeometricObject(String data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return data;
    }

}

Without the Override, you are using Object.toString(). Object's toString prints out the class name and the hashcode of the object, as you have observed.

user63762453
  • 1,734
  • 2
  • 22
  • 44
Jonah Graham
  • 7,890
  • 23
  • 55
  • Ahhhh thank you! To be honest though, I'm not 100% sure why adding that code gave me the correct output. Is there anyway you could explain at all. – RiFF RAFF Oct 11 '15 at 06:21
  • @RiFFRAFF I recommend heading over to http://www.tutorialspoint.com/java/number_tostring.htm and have a bit more of a play – Jonah Graham Oct 11 '15 at 08:09
0
 System.out.println(o);

When you call System.out.println, That apparently calls the toString() method of your Object. Since you didn't ovveride toString(), it calls the default implementation. Ovveride toString() method to print as you wish.

public class GeometricObject {

....


 @Override
    public String toString() {
       // return  string representation of your object.
    }


}

To starts with :What is the best standard style for a toString implementation?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307