-4

I am having a hard time finding out how to write my toString Method to get the output of each of my bears in my program. I want the output to show "Race - Points - TotalPoints". But can't manage to get it right even though the rest of the code seems to compile.

Do i need to have the toString defined in both classes or what am I missing? I have checked a couple of other questions that are resembling and that seems to be an alternativ? But how is it most effectively implemented?

First off the bear class:

import java.util.ArrayList;

public class Bear {

    public static void main(String[] args) {

        Bear b = new Bear("Sebastian", 100, "Brownbear");
        ArrayList <Bear> bears = new ArrayList<Bear>();
        bears.add(b);

    }

    private String name;
    private int points;
    private String race;


    public Bear(String name, int points, String race) {
        this.name = name;
        this.points = points;
        this.race = race;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRace() {
        return race;
    }

    public void setRace(String race) {
        this.race = race;
    }

    public int getInitialPoints() {
        return points;
    }

    public int getPoints() {
        int oldPoints = points;
        points /= 2;
        return oldPoints;
    }
}

Secondly the BearCollection class:

import java.util.ArrayList;

public class BearCollection {

    ArrayList <Bear> bears = new ArrayList<Bear>();

     int totalPoints = 0;

    public void add (Bear b) {

        for (Bear inCollection : bears) {
            if(b.getName().equals(inCollection.getName())) {
                return;
            }
        }

        for (Bear inCollection : bears)
        if (b.getRace().equals(inCollection.getRace())) {

            for(int i =  bears.size(); i > 0; i --) {
                if(bears.get(i).getRace().equals(b.getRace())) {
                    b.getPoints(); 
                    i = 0;
                } 
            }
        }

        totalPoints += b.getInitialPoints();


        bears.add(b) ;
    }

public String toString(){

    return ;
}
Joe C
  • 15,324
  • 8
  • 38
  • 50
  • Have a look at this http://stackoverflow.com/questions/18129505/how-can-i-override-the-tostring-method-of-an-arraylist-in-java – freedev Feb 13 '17 at 22:46
  • 2
    With all due respect, you're not seriously telling us that you're having issues with `return ;`, are you? – Joe C Feb 13 '17 at 22:46
  • Of course I understand that my tostring is completely blank at the moment I just don't understand what I should put there to get the right output. I just put the toStringmethod there as to show where I thougth it was supposed to go. (but I am a complete beginner) – wilhelmeriksson Feb 13 '17 at 22:48
  • You already know how to iterate through your list of bears as you already do it in the code. So whats stopping you from iterating through it and printing whatever info you want from the current bear iteration? – OH GOD SPIDERS Feb 13 '17 at 22:52
  • Are you stuck on some specific problem? Or are you asking us to write code for you? – shmosel Feb 13 '17 at 22:59
  • I am stuck on the problem that I do not understand what I should put in my toString return statement. I'm just asking for help with how to do it as all I ever have used before is sysout. So yes, I guess I am asking if someone could tell me (and explain would be helpful) how to build the correct toString. – wilhelmeriksson Feb 13 '17 at 23:03

3 Answers3

0

overriding the toString in Bear class would resolve the issue.

Sneha
  • 39
  • 6
0

As you were told, just override the toString method. For performance use StringBuilder, rather than String concatenation.

import java.util.*;

public class ans{
    public static void main(String[] args){
    Bears bears = new Bears();
    bears.add(new Bear());
    bears.add(new Bear());
    bears.add(new Bear());

    System.out.println(bears);
    }
}

class Bear{
    public String toString(){
    return "I am a bear";
    }
}

class Bears{
    private ArrayList<Bear> bears = new ArrayList<Bear>();

    public void add(Bear bear){
    bears.add(bear);
    }

    public String toString(){
    StringBuilder str = new StringBuilder();
    if(!bears.isEmpty()){ // If there is no bears, return empty string
        str.append(bears.get(0)); // Append the first one
        for(int index = 1; index < bears.size(); index++){ // For all others
        str.append(" - "); // Append a separator and the bear string
        str.append(bears.get(index));
        }

    }
    return str.toString();
    }
}

Edit To print A-B-C-D, just associate every item with a separator except one. A(-B)(-C)(-D) or (A-)(B-)(C-)D. You could add easily a beginning and a end mark.

Black Arrow
  • 375
  • 3
  • 12
0

If you need to print out the entire collection of Bears, you'd need to give Bear a toString, something like:

return "Bear("+name+","+points+","+race+")";

Then, in the toString of BearCollection, just write a for each loop in the toString to go through and call toString on each bear in the collection, printing them out.

Apitosu
  • 86
  • 5