java programming language The WorldSeriesdata file is just a

(java programming language)

(The WorldSeries.data file is just a txt file with every winner of the world series on it written from 1904 to 2009)

EXAMPLE OF WorldSeries.data FILE

Boston Americans
New York Giants
Chicago White Sox
Chicago Cubs
Chicago Cubs

Use the attached WorldSeries.data file which contains the chronological list of world series winners from 1903 to 2009.

Read the data from the file into an array. When the user enters the name of the team, the program should step through the array counting the number of times the selected team appears.

The first line in the file is the name of 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 1904 or 1994, so the file contains no entires for those years.

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 to 2009.

Solution

//In the code below we took the approach of using java 8 syntax which could easily be transformed based on your JDK version requirement.//

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

    Path worldSeriesWinners = Paths

            .get(\"src/main/resources/com/levelup/java/exercises/beginner/WorldSeriesWinners.txt\")

            .toAbsolutePath();

    // read all lines of file into arraylist

    List<String> winners = Files.lines(worldSeriesWinners).collect(

            Collectors.toList());

    // 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 = winners.stream()

            .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.in);

    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

    keyboard.close();

    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.\");

}

(java programming language) (The WorldSeries.data file is just a txt file with every winner of the world series on it written from 1904 to 2009) EXAMPLE OF Worl
(java programming language) (The WorldSeries.data file is just a txt file with every winner of the world series on it written from 1904 to 2009) EXAMPLE OF Worl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site