World Series Champions

The problem

Write a program that lets the user enter the name of a team and then displays the number of times that team has won the world series in the time period from 1903 through 2009. The WorldSeriesWinners.txt file contains a chronological list of the world series winning teams from 1903 through 2009 the first like in the file is the name of the team that won in 1903 and last line is the name of team that won in 2009. Note that the world series was not played in 1904 and 1994.

There are many solutions to this problem, you could import a file into a guava multimap, into array or array list. In the code below we took the approach of using java 8 syntax which could easily be transformed based on your JDK version requirement.

Breaking it down

public static void main(String args[]) throws IOException {

    Path worldSeriesWinners = Paths

    // read all lines of file into arraylist
    List<String> winners = Files.lines(worldSeriesWinners).collect(

    // ask user to enter a team
    String teamName = getTeamName();

    // use a stream to filter elements based on user input
    // count the number of elements left
    long numberOfWins =
            .filter(p -> p.equalsIgnoreCase(teamName)).count();

    // show output
    output(teamName, numberOfWins);


 * This method should return a string which represents a world series
 * champion entered by a user.
 * @return string
public static String getTeamName() {

    Scanner keyboard = new Scanner(;

    System.out.println("World Series Champions");
    System.out.print("Enter the name of a team: ");

    // Return the name input by the user.
    String team = keyboard.nextLine();

    // close scanner

    return team;

 * This method will format the output to the user
 * @param teamName
 * @param numberOfWins
public static void output(String teamName, long numberOfWins) {

    // Display the result
    System.out.println(teamName + " won the World Series a total of "
            + numberOfWins + " times.");



World Series Champions
Enter the name of a team: Philadelphia Athletics
Philadelphia Athletics won the World Series a total of 5 times.

Level Up

  • Modify the program to allow a user to enter in a team until they enter in -1.