dylan dylan - 17 days ago 5
Java Question

Class toString() method that returns a formatted array of objects

My situation is I have to make a toString() method for a class that basically just takes a bunch of objects from another class and puts all the objects into an arraylist. The class that makes the arraylist needs this toString() method to return all the arraylist's objects in a formatted string which will be ready to display in the console. The class that creates the objects which are put into the arraylists has a toString() method of its own which returns a nicely formatted string of all the data, perhaps I could call this toString() method for each object in the other class's toString() method? Here's the code:

Class that creates base objects:

public class Batter {

private String firstName;
private String lastName;
private int atBats;
private int hits;
private int doubles;
private int triples;
private int homeRuns;

public Batter(String fName, String lName){
firstName = fName;
lastName = lName;
}
public void setBats(int batCount){
atBats = batCount;
}
public void setHits(int hitCount){
hits = hitCount;
}
public void setDoubles(int doubleCount){
doubles = doubleCount;
}
public void setTriples(int tripleCount){
triples = tripleCount;
}
public void setHR(int homeRunCount){
homeRuns = homeRunCount;
}

public double getAve(int hits, int bats){
double average = hits / bats;
return average;
}
public String getName(){
String fullName = this.lastName + "," + this.firstName;
return fullName;
}
public boolean equals(){

return true;
}
public String toString(){
return "Player: "+getName()+"\nAt Bats: "+this.atBats+"\nHits: "+this.hits+"\nDoubles: "+this.doubles+"\nTriples: "+this.triples+"\nHome Runs: "+this.homeRuns;
}
}


Class that puts the objects made from the above class into an arraylist - this is where I'm not sure what to do. The code I need to write will be in the toString() method of this class. Again, its objective is to return all the object's data in a nicely formatted string, maybe somehow using those object's toString() method created in the other class:

public class BatterDB
{
private ArrayList<Batter> theBatters = new ArrayList<Batter>(); // ArrayList of Batters
private int num; // int to store logical size of DB

// Initialize this BatterDB
public BatterDB()
{
num = 0;
}


public void addBatter(Batter b)
{
theBatters.add(b);
num++;
}


public Batter removeBatter(Batter b)
{
return theBatters.remove(0);
}

// Return logical size of the DB
public int numBatters()
{
return theBatters.size();
}


public Batter findBatter(String fName, String lName)
{
return theBatters.get(0); //change
}


public void sortName()
{
}


public void sortAve()
{
}


public String toString()
{
//Where I'm lost
}


public String toStringFile()
{
return "toStringFile()";
}

}

Answer

What you want to do is turn the List theBatters into a String. In your toString method at BatterDB, do this:

return theBatters.stream()    // get all of them in a stream
    .map(b -> b.toString())    // turn them into strings
    .collect(Collectors.joining(", "))    // join them with commas
                                          // (also can be anything, like "\n" for new lines)
Comments