Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help writing the Java code that allow user to enter the name of the team to

ID: 3841640 • Letter: N

Question

Need help writing the Java code that allow user to enter the name of the team to check the number of time this term win (list of team is in a text and display the number of time the name of team appear) so Java should read from the text file and if the user enter the team name it should give the number of time the team win and if the user enter the name of the team that is not in the text it should give an error message. 4. The file named world series a chronological a the file is the name of contains series winning teams from 1903 first line in the team that won in 1903, and the last line is the name of the team that won in 2009. Note that the World Series was not played in 19o4 or 1994.) Write a program winner Frequency.1ava that has a main method that lets the user enter the name of a team and then displays to the screen the number of times that team has won the world Series in the time period from 1903 through 2009. Your program calls a method countwins that takes a name of team and the filename as parameter and returns how many time this teams won. The method has the following signature: public static int countWins (String teamName, string fileName) Test your program with 3 different team names: o 2 form the sample file provided world series winners .txt o 1 team name that is not in the file,

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class WinnerFrequency {

   public static int countWins(String teamName, String fileName) {
       int counter = 0;
      
       try {
           /*
           * Open the file and parse each line, count the occurence of teamName
           */
           BufferedReader buffer = new BufferedReader(new FileReader(fileName));
          
           String inputLine;
           while ((inputLine = buffer.readLine()) != null) {
               if (inputLine.equalsIgnoreCase(teamName)) {
                   counter++;
               }
            }      
          
           buffer.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
      
       return counter;
   }
  
   public static void main(String args[]) {
       Scanner sc = new Scanner(System.in);
       String fileName = "world_series_winners.txt";

       System.out.print("Please Enter A Team Name : ");
       String teamName = sc.nextLine();
       sc.close();
      
       int wins = countWins(teamName, fileName);
       System.out.println("No. of times " + teamName + " has won are " + wins);
      
      
       /*
       * Testing with three cases
       * TODO - Include Titans in the input file and don't include Templars
       */
       teamName = "Titans";
       wins = countWins(teamName, fileName);
       System.out.println("No. of times " + teamName + " has won are " + wins);
      
       teamName = "Templars";
       wins = countWins(teamName, fileName);
       System.out.println("No. of times " + teamName + " has won are " + wins);
      
      
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote