title: How to detect duplicates in ArrayList Java
body: <p>I want to make java code that if a country already has 5 teams they cannot create more teams.</p>
  <p>For example:</p>
  <p>User inputs country name <em>asd</em>, then inputs team name, etc. and user does it 4 more times.</p>
  <p>The sixth time when user inputs <em>asd</em> country name it will return an error, but if the user inputs another country name they can make another team.</p>
  <p>I've tried using <code>arrayList.size()</code> but I still couldn't get it working.</p>
  <p>This is my code</p>
  <pre><code>import java.util.*;
  public class Main {
  Scanner sc = new Scanner(System.in);
  ArrayList&lt;Team&gt; teams = new ArrayList&lt;&gt;();
  
  public Main() {
      int choose = 0;
      do {
          choose = displayMenu(); sc.nextLine();
          
          switch (choose) {
          case 1:
              createTeam();
              break;
          case 2:
              addMatch();
              break;
          case 3:
              showStat();
              break;
          case 4:
              showRank();
              break;
          case 5:
              eraseTeam();
              break;
  
          }
          
      } while (choose != 6);
      
  }
  
      private void createTeam() {
      String country, teamName, coachName, participantName;
      System.out.print(&quot;Enter Country Name : &quot;);
      country = sc.nextLine();
      do {
          System.out.print(&quot;Enter Team Name : &quot;);
          teamName = sc.nextLine();
          participantName = getParticipantName();
          System.out.println(&quot;Enter Coach Name : &quot;);
          coachName = sc.nextLine();
          Team team = new Team(country, teamName, coachName, participantName);
          teams.add(team);
      } while (teams.size()&gt;=5);
  }
  </code></pre>
  <p>Sorry for my bad English.
  Thank you in advance</p>