0

The code below reads from a file. The file contains around 3000 lines with 193 digits separated by space in each line.

void read(){

    String fileName = "ocr_train.txt";
    String line = null;
    float[] vertices = new float[200];

    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            //System.out.println(line);

            String[] parts = line.split(" ");
            for (int i = 0; i < parts.length; i++){
                if(parts[i] != null && parts[i].length() > 0){
                    vertices[i] = Float.parseFloat(parts[i]);
                }
            }
            Points p = new Points(vertices);
            System.out.println(p);
            full.add(p);
        }   
        bufferedReader.close();         
    }

Here, when I try printing p, it gives me values like Points@10bf683, Points@d322f5 etc What is wrong with this step?

enthusiast
  • 29
  • 1
  • 4
  • Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Jason Mar 08 '16 at 01:57
  • Check your Points class to have the toString() method or not. If not, please override it and print what you want inside it. Hope this help. – Kenny Tai Huynh Mar 08 '16 at 02:07

2 Answers2

1

Assuming Point is a user defined class and contains a reference to vertices array, we need to override toString() method in Point class (in order for System.out.println() method to show some meaningful value as the default one shows HashCode of the object). Implementation of toString() would look like this:

public class Point {

    int[] vertices;
    //other code
    @Override
    public String toString(){
        if(null != vertices){
            return Arrays.toString(vertices);
        }else{
            return null;
        }
    }

}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

I think you need override Point.toString().

When you using System.out.println(p); you're actually using System.out.println(p.toString());

Except for eight basically data types, other object using System.out.println() will print their address if you don't override toString()

mmgross
  • 3,064
  • 1
  • 23
  • 32
horo.z
  • 1
  • 1