Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading