0

I have a small problem with printing out strings that are stored in an object. The object is stored in an ArrayList.

I have three clases that I use im my program:

Friend Class:

package one;

public class Friend implements InterfaceFriend {

    private String name;
    private String email;
    private String phone;

    public Friend(String name, String phone, String email) {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String getPhone() {
        return phone;
    }

    @Override
    public String getEmail() {
        return email;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setPhone(String phone1) {
        phone1 = phone;
    }

    @Override
    public void setEmail(String email1) {
        email1 = email;
    }

    @Override
    public void setName(String name1) {
        name1 = name;
    }

}

FriendInterface:

package one;

public interface InterfaceFriend {
    String getPhone(); // Returns phone number.

    String getEmail(); // Returns email.

    String getName(); // Returns name.

    void setPhone(String phone); // Sets phone.

    void setEmail(String email); // Sets email.

    void setName(String name); // Sets name.
}

and Test Class:

package one;

import java.util.ArrayList;
import java.util.List;

public class FriendTest {

    static List<Friend> friends;
    static Friend friend;


    public static void main(String args[]) {

        friends = new ArrayList<Friend>();

        friend = new Friend("Jane Doe", "085-5555555", "[email protected]");
        friends.add(friend);

        friend = new Friend("John Doe", "085-1111111", "[email protected]");
        friends.add(friend);

        friend = new Friend("Paul Weller", "085-3333333", "[email protected]");
        friends.add(friend);

        System.out.println("Friends added to list:");

        System.out.println(friends.toString());

    }
}

The problem is that when I am running the System.out.println(friends.toString());from the Test Class i am getting this: Friends added to list: [one.Friend@38f0b51d, one.Friend@4302a01f, one.Friend@615e7597]

Instead the Strings with the values that I want. Any help appreciated.

2
  • 3
    Override the toString() method in your Friend class. Commented Nov 19, 2013 at 18:07
  • 2
    In addition to toString() you also need to make sure your Friend(String,String,String) constructor is implemented - because that seems to be blank. Commented Nov 19, 2013 at 18:08

4 Answers 4

2

You'll need to override the toString() method in the Friend class as commented already, but you also need to complete the constructor that you're using.

public Friend(String name, String phone, String email) {
    this.name = name;
    this.phone = phone;
    this.email = email;
}

Moreover, the code in your setters are backwards.

Sign up to request clarification or add additional context in comments.

Comments

1

In you friend class you need to override toString()

public class Friend implements InterfaceFriend {
    ...
    ...

    public String toString(){
        return name + " " + email + " " + phone; // or whatever format you want printed
    }
}

Comments

0

you simply override toString method. place this inside of Friend class. problem solved..

public String toString(){
   // return  your Strings..
}

Comments

0

Override to toString() in your class, to what output suits you. The reason you're getting that output is because the default toString() provided in objects is a hash code.

public String toString(){
   return name; //assuming you only want to display the name else edit it

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.