title: How to count numbers of names from a file using String and File classes in Java?
body: <p>I am trying to learn Objects and Classes. Here is my problem: Each line of the file holds the outcome of a single game: the names of 2 teams together with the scores. I created a file using Notepad which looks like this:</p>
  
  <pre><code>Panthers  5  Tigers    5
  
  Sky       2  Panthers  5
  
  Tigers    1  Sky       0
  
  Sky       2  Panthers  1
  
  Tigers    1  Sky       4
  </code></pre>
  
  <p>I tried to read to the file and count number of teams in the file. For example, in this file, there are 3 different teams. "Panthers, Tigers, Sky". But without program the computer doesn't know how many of the teams.</p>
  
  <p>I created a string array in the Soccer class</p>
  
  <pre><code>private n;
  private String[] team; 
  </code></pre>
  
  <p>I use File and Scanner in Soccer(String filename) constructor of the Soccer class to read the file</p>
  
  <pre><code>File inputFile = new File(filename);
  Scanner inFile = new Scanner(inputFile);
  n = 1;
  
  team = new String[n]; // Create a String array of n
  
  team[n-1] = inFile.next();
  
  while(inFile.hasNext()){
  
      String num = inFile.nextInt(); //For the scores next to team's name 
  
      String name = inFile.next(); // Read another name and store it to String name
  
      while(checkDuplicate(name)) // Check if name and team[i=0 to n] are the same return true
     {
        num = inFile.nextInt();
        name = inFile.next();
     }
  
       n = n + 1; // Increase string array size to 1
       team = new String[n];
       team[n - 1] = name;
  
  }
  
      System.out.print("Number of teams: " + n);
  
      System.out.print("Name of the teams: ");
      for(int i = 0; i &lt; n; i++)
         System.out.print(team[i] + " ");
  </code></pre>
  
  <p>Down here is my checkDuplicate() helper method</p>
  
  <pre><code>private boolean checkDuplicate(String name) 
  {
      int count = 0;
      for(int i = 0; i &lt; n; i++)
      {
          if(name.equals(team[i]))
            count = count + 1;
      }
  
      if(count &gt; 1)
         return true;
      return false;
  }
  </code></pre>