Reputation: 29
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?
Upvotes: 0
Views: 117
Reputation: 1
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()
Upvotes: 0
Reputation: 30839
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;
}
}
}
Upvotes: 1