Reputation: 9865
I have array in String
. I want to use for
loop to print all of the objects. I was under the impression this would be done by making for loop then returning the String
. I am not sure what I need to change to accomplish this.
Following is my effort:
public class SolarSystem {
private Planet[] planets;
private int position = 0;
public SolarSystem(int size) {
planets = new Planet[size];
}
public void add(Planet planet) {
planets[position] = planet;
position++;
}
public String toString(){
for(int i = 0; i < planets.length; i++){
}
return toString();
}
}
ADDED PLANET CLASS
public class Planet {
String name;
int moons;
public Planet(String name, int moons)
{
this.moons = moons;
this.name = name;
}
public String toString() {
return "The Planet " + name + " Has " + moons + " Moon(s) \r\n ";
}
}
Upvotes: 0
Views: 251
Reputation: 44813
If you have this overidden method in your planet class
public String toString(){
StringBuilder result = new StringBuilder();
String NEW_LINE = System.getProperty("line.separator");
result.append(" planetProperty1: ").append (planetProperty1).append(NEW_LINE);
result.append(" planetProperty2: ").append (planetProperty2).append(NEW_LINE);
return result.toString();
}
then from the calling class you can iterate over the collection calling
System.out.println (planets[i].toString());
Upvotes: 0
Reputation: 129
Override toString() Method
@Override
public String toString() {
return "SolarSystem [te=" + Arrays.toString(te) + ", position=" + position + "]";
}
Upvotes: 0
Reputation: 140
public String toString() {
StringBuilder mainresult = new StringBuilder();
for(int i = 0; i < planets.length; i++){
StringBuilder result = new StringBuilder();
String newLine = System.getProperty("line.separator");
result.append( planets[i].getClass().getName() );
result.append( " Object {" );
result.append(newLine);
//determine fields declared in this class only (no fields of superclass)
Field[] fields = this.getClass().getDeclaredFields();
//print field names paired with their values
for ( Field field : fields ) {
result.append(" ");
try {
result.append( field.getName() );
result.append(": ");
//requires access to private field:
result.append( field.get(this) );
} catch ( IllegalAccessException ex ) {
System.out.println(ex);
}
result.append(newLine);
}
result.append("}");
//if it is the first one then no new line added
if(i==0)
{
mainresult.append(result.toString());
continue;
}
mainresult.append(newLine);
mainresult.append(result.toString());
}
return mainresult.toString();
}
Upvotes: 1
Reputation: 16158
Override toString()
method in Planet
class, and use below code :
public String toString(){
String result = "";
for(int i = 0; i < planets.length; i++){
result += planets[i].toString(); // append comma if you need to separate it,
// and most of all handle nulls
}
return result;
}
Upvotes: 1
Reputation: 34146
You can try using a StringBuilder
:
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < planets.length; i++){
sb.append(planets[i].getName()); // getName() or toString()
sb.append("\n");
}
return sb.toString();
}
Upvotes: 2