JAVA 7 IDE PROGRAMMING LANGUAGE The WorldSeriesdata file is

(JAVA 7 IDE 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

import java.util.Scanner;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WorldSeries {
void calculateWin(String filename) throws IOException
{
File file = new File(filename);
try {

Scanner sc = new Scanner(System.in);
System.out.println(\"Enter the name of the team to know the stats :\");
String team = sc.nextLine();

Scanner sc2 = new Scanner(file);
int win_count=0;
while (sc2.hasNextLine()) {
String file_team_name = sc2.nextLine();
System.out.println(file_team_name);
if(file_team_name.trim().equals(team.trim()))
{
win_count++;
}
}
System.out.println(team+\" has won the world series \"+win_count+\" times\");
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
try {
WorldSeries ob = new WorldSeries();
String filename = \"F:\\\\WorldSeries.txt\";
ob.calculateWin(filename);
} catch (IOException ex) {
Logger.getLogger(Pet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

(JAVA 7 IDE 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 O
(JAVA 7 IDE 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 O

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site