简体   繁体   中英

Java Arraylist search for specific string and if found then print

having a problem with this java homework where I input Olympians into the array list: Name, Gold, silver, bronze medals. If the name of the olympian searched is found then print out the name and the medals he got, else print an error message. This is the code so far to input the name and medals.

   public class Analyser {

     private ArrayList <Olympian> olympians;

        public Analyser() {
            olympians = new ArrayList<Olympian>();    
        } 

        public void addOlympian (String name, int gold, int silver, int bronze) {
            olympians.add(new Olympian(name, gold, silver, bronze));       
        }
    }

Before asking for help with the code itself, you need to figure out a few things:

  • What parameters your searchOlympian() function should take.
  • How you're going to find the desired Olympian in the ArrayList , given the name.
  • How you're going to figure out if the Olympian isn't in your list.

A few hints:

  • String.equals(String otherString) will return true if the strings are equal.
  • for (Olympian olympian : olympians) will help you go through each Olympian .
  • If you go through the above loop without finding the Olympian , then the Olympian isn't in the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM