Saint
Saint

Reputation: 480

Outputing string values from objects saved in an ArrayList

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.

Upvotes: 0

Views: 142

Answers (4)

jantristanmilan
jantristanmilan

Reputation: 4368

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

}

Upvotes: 0

Bobbo
Bobbo

Reputation: 315

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.

Upvotes: 2

subash
subash

Reputation: 3115

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

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

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209072

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
    }
}

Upvotes: 1

Related Questions