Question: Java How can i modify my program to allow the user to run the program as many times as possible until a sentinel value of
Java
How can i modify my program to allow the user to run the program as many times as possible until a sentinel value of zero (0) has been entered for the year . Program works just fine, just need help changing that.
Program outline: Write a program that reads this file only once and creates a HashMap in which the keys are the years and each keys associated value is the name of the team that won that year. The program should also create a HashMap in which the keys are the names of the teams and each keys associated value is the number of times the team has won the World Series.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class WorldSeries {
private HashMap wininingYearTeamsMap;
private HashMap movieWininingCountMap;
public WorldSeries() {
wininingYearTeamsMap = new LinkedHashMap();
movieWininingCountMap = new LinkedHashMap();
}
public static void main(String[] args) {
WorldSeries worldSeries = new WorldSeries();
worldSeries.readInputFile("Program3.txt");
worldSeries.readUserInput();
}
private void readUserInput () {
Scanner userInput = new Scanner(System.in);
int year;
do {
System.out.print("Enter a year between 1903 and 2019, \"0\" to stop: ");
year = userInput.nextInt();
String teamName = getTeamNameBasedOnYear(year);
if (teamName != null && !teamName.equalsIgnoreCase("")) {
int winningCount = getWinningCount(teamName);
System.out.println(teamName+ " won the World Series in: "+year);
System.out.println("Total number of times "+teamName+" won the World Series is: "+winningCount);
} else {
System.out.println("No team won the World Series in: "+year);
}
} while (year < 1903 || year > 2019);
userInput.close();
}
private void readInputFile(String inputFileName) {
Scanner sacnnerReader = null;
try {
File file = new File(inputFileName);
if (file.exists()) {
int year = 1903;
sacnnerReader = new Scanner(file);
while (sacnnerReader.hasNextLine()) {
String teamName = sacnnerReader.nextLine();
if (year == 1904 || year == 1994 ) {
year++;
}
wininingYearTeamsMap.put(year, teamName);
if (movieWininingCountMap.get(teamName) != null) {
movieWininingCountMap.put(teamName, movieWininingCountMap.get(teamName)+1);
} else {
movieWininingCountMap.put(teamName,1);
}
year++;
}
} else {
System.out.println("Input File not found");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sacnnerReader != null) {
try {
sacnnerReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private String getTeamNameBasedOnYear(int year) {
if (wininingYearTeamsMap.get(year) != null) {
return wininingYearTeamsMap.get(year);
} else {
return null;
}
}
private int getWinningCount(String teamName) {
if (movieWininingCountMap.get(teamName) != null) {
return movieWininingCountMap.get(teamName);
} else {
return 0;
}
txt file
Boston Americans New York Giants Chicago White Sox Chicago Cubs Chicago Cubs Pittsburgh Pirates Philadelphia Athletics Philadelphia Athletics Boston Red Sox Philadelphia Athletics Boston Braves Boston Red Sox Boston Red Sox Chicago White Sox Boston Red Sox Cincinnati Reds Cleveland Indians New York Giants New York Giants New York Yankees Washington Senators Pittsburgh Pirates St. Louis Cardinals New York Yankees New York Yankees Philadelphia Athletics Philadelphia Athletics St. Louis Cardinals New York Yankees New York Giants St. Louis Cardinals Detroit Tigers New York Yankees New York Yankees New York Yankees New York Yankees Cincinnati Reds New York Yankees St. Louis Cardinals New York Yankees St. Louis Cardinals Detroit Tigers St. Louis Cardinals New York Yankees Cleveland Indians New York Yankees New York Yankees New York Yankees New York Yankees New York Yankees New York Giants Brooklyn Dodgers New York Yankees Milwaukee Braves New York Yankees Los Angeles Dodgers Pittsburgh Pirates New York Yankees New York Yankees Los Angeles Dodgers St. Louis Cardinals Los Angeles Dodgers Baltimore Orioles St. Louis Cardinals Detroit Tigers New York Mets Baltimore Orioles Pittsburgh Pirates Oakland Athletics Oakland Athletics Oakland Athletics Cincinnati Reds Cincinnati Reds New York Yankees New York Yankees Pittsburgh Pirates Philadelphia Phillies Los Angeles Dodgers St. Louis Cardinals Baltimore Orioles Detroit Tigers Kansas City Royals New York Mets Minnesota Twins Los Angeles Dodgers Oakland Athletics Cincinnati Reds Minnesota Twins Toronto Blue Jays Toronto Blue Jays Atlanta Braves New York Yankees Florida Marlins New York Yankees New York Yankees New York Yankees Arizona Diamondbacks Anaheim Angels Florida Marlins Boston Red Sox Chicago White Sox St. Louis Cardinals Boston Red Sox Philadelphia Phillies New York Yankees San Francisco Giants St. Louis Cardinals San Francisco Giants Boston Red Sox San Francisco Giants Kansas City Royals Chicago Cubs Houston Astros Boston Red Sox Washington Nationals
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
