user15541234
user15541234

Reputation:

Return certain values from the arraylist

Trying to return only the references(IW1, SS2) in the getUFFleet() however it is returning every single value of the toString(). MethodsgetUFFleet() and setupForces are from the same class while the toString from another class FYI.

public String getUFFleet()
    {
        System.out.println(ForceDetails.toString());
        
        return "No forces in UFF";
      
    }
private void setupForces()
    {
        ForceDetails.add(new starShip("IW1","Twisters",200,200,10,0,0,"No","Wing" +"\n"));
        ForceDetails.add(new starShip("SS2","Enterprise",300,200,0,10,20,"No","Starship"));
  
    }
   public String toString()
    {
        String s;
        s = "\nForce reference: " + FleetRef + "\nName: " + FullName +
            "\nActivation Fee: " + ActivationFee +"\nStrikers: "
            + Strikers + "\nLaser Canons: " + LaserCanon + "\nPhotonTorpedoes: "
            + PhotonTorpedoes + "\nStregth: "+ BattleStrength
            +"\nCloaking: " + Cloaking + "\nForce Type: " + ForceType +"\n";
        return s;
    }

Upvotes: 0

Views: 90

Answers (1)

Yusuf gndz
Yusuf gndz

Reputation: 137

You can try to loop trough:

public String getUFFleet(){
    ForceDetails.forEach((starShip data) -> {
        System.out.println(data.getFleetRef());
      }
    );

    return "No forces in UFF";
}

as extra information about forEach loops see following link: Foreach loop in java for a custom object list

Upvotes: 1

Related Questions