0

I have an array which has objects of people in a football team. It holds information such as their first name, second name and address. When i use the code shown below the text file contains values like this: member@29086037

The code is shown below:

try
{
    PrintWriter pr = new PrintWriter ("memberDetails.txt");
    for (int i = 0; i < collection.length; i++)
    {
    pr.println(collection[i]); 
    }
    pr.close();
} catch (FileNotFoundException ex)
{
    System.out.println(ex.getMessage());
    System.out.println("in" + System.getProperty("user.dir"));
    System.exit(1);
}

What am I doing wrong?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
sam
  • 3
  • 1

4 Answers4

3

When you see that malarky with the numbers and class name like that, it means you haven't overriden your toString() method, so it defaults to Object.toString().

So, override the public String toString() method on your member class.

Hailey
  • 318
  • 1
  • 6
1

When you do pr.println(collection[i]); as you didn't override it, you print Object::toString which represents the object in this way by default:

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

To print each field, use properties of the object, for example:

collection[i].getName(); 
collection[i].getAddress(); 

Other option, is to override toString() method of member.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • *When you do pr.println(collection[i]); you print the object reference* this is not what happens. Instead, it calls `collection[i].toString()`. From the output, seems like the class doesn't override this method, so this boils down to execute `super.toString()` which ends in `Object#toString`. – Luiggi Mendoza Apr 08 '15 at 22:30
  • I always thought by default toString prints the internal reference in java, fixed,... Thanks! ;) – Jordi Castilla Apr 08 '15 at 22:36
  • if my array is not full and i try to write to a file i get a null pointer exception. How could i fix this? – sam Apr 08 '15 at 23:43
  • 1) create the array with exact size or 2) check if `collections[i] == null` before reading the line, or 3) better, use a premade `Collection` such as [ArrayList](http://javarevisited.blogspot.com.es/2011/05/example-of-arraylist-in-java-tutorial.html) which will manage the sizes for you. – Jordi Castilla Apr 09 '15 at 07:22
0

You have to provide the path to the file correctly. I would suggest creating a file object and then pass it to Printwriter. This way you can also make sure if File exist before assigning it to printwriter.

happyHelper
  • 119
  • 8
0

As others have pointed out, assuming you have a class that models the players, you should provide a toString() implementation in the class. For example:

public class Player {
    private String firstName;
    private String lastName;
    private String address;
    ...
    @Override
    public String toString() {
        return String.format("Name: %s, %s. Address: %s", lastName, firstName, address);
    }
}

After that's been done, it becomes trivial to write the player information into a file. Using an utility library such as Google's Guava, the solution simplifies into a one-liner:

Files.write(Joiner.on(StandardSystemProperty.LINE_SEPARATOR.value())
                  .join(collection),
            new File("memberDetails.txt"),
            Charsets.UTF_8);
Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30