1

These are my 2 classes, they work. I want to add Member into the ArrayList of Lan. My problem is, I can't seem to print out the members that are set in the ArrayList of Lan, the name and alias or just the name. when I print this comes: Member@1279d2f, any ideas? :S

import java.util.ArrayList;

public class Lan
{

private ArrayList<Member> members;



    public Lan()
    {
      members = new ArrayList<Member>();

    }

    public void addMember(Member newMember)
    {
        if(newMember.getBalance() >= 200)
        {
            this.members.add(newMember);
        }
        else
        {
            System.out.println("You dont have enough money to enter");
        }

    }
   public void printMembers(){

       System.out.println("People attending");
       for(Member member : members){

          System.out.println(member);
       }

   }

}


public class Member
{

    private String name;
    private String alias;
    private int balance;


    public Member(String fullName, String nickname)
    {
        this.name = fullName;
        this.alias = nickname;
        this.balance = 0;
    }
    public void setBalance(int account)
    {
        if(account > 0)
        {
            balance = balance + account;
        }
        else
        {
            System.out.println("You have not entered a valid number");
        }
    }

    public String getName()
    {
        return name;
    }
    public String getAlias()
    {
        return alias;
    }

    public int getBalance()
    {
        return balance;
    }
}

1 Answer 1

2

You must define a toString() method for your Member class:

public class Member { 

  private String name;
  private String alias;
  private int balance;


  public Member(String fullName, String nickname)
  {
      this.name = fullName;
      this.alias = nickname;
      this.balance = 0;
  }
  public void setBalance(int account)
  {
      if(account > 0)
      {
          balance = balance + account;
      }
      else
      {
          System.out.println("You have not entered a valid number");
      }
  }

  public String getName()
  {
      return name;
  }
  public String getAlias()
  {
      return alias;
  }

  public int getBalance()
  {
      return balance;
  }

  @Override
  public String toString() {
    return this.name + "\t" + this.balance;
  }

}

Without a toString() method for your custom class, you will print its location in memory. If there's other information you want to print in Member, modify the method that I've provided for you.

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

4 Comments

Don't rely on it being its location in memory. While that was one of the implementations, it's not something you should assume is true.
When i compile i get missing return statement on the @Override public String toString() { System.out.println(this.name + "\t" + this.balance); }
Ahh, alright. In this case, because im returning both in a toString, should i just remove return balance / return name?
The 'toString()' method is just the standard textual representation of your class. If, for example, you wanted to represent your member as the name and alias, you would 'return this.name + " " + this.alias;'

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.