Question

Java How can i modify my program to allow the user to run the program as...

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 key’s 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 key’s 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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here is the updated class that will keep on looping until user enters 0

Hope this helps

let me know for any help with any other questions,

================================================================

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class WorldSeries {
    private static HashMap<Integer, String> wininingYearTeamsMap;
    private static HashMap<String, Integer> movieWininingCountMap;

    public WorldSeries() {
        wininingYearTeamsMap = new LinkedHashMap();
        movieWininingCountMap = new LinkedHashMap();
    }

  public static void main(String[] args) {
        WorldSeries worldSeries = new WorldSeries();
        worldSeries.readInputFile("D:\\series.txt"); // i updated the file name when i was testing
        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();
            if(year!=0){
                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!=0); // loop until year is not zero
        userInput.close();

        System.out.println("Bye!");
    }

    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;
        }
    }
}

================================================================

Add a comment
Know the answer?
Add Answer to:
Java How can i modify my program to allow the user to run the program as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3.1 - 3.5 Certain machine parts are assigned serial numbers. Valid serial numbers are of...

    Python 3.1 - 3.5 Certain machine parts are assigned serial numbers. Valid serial numbers are of the form: SN/nnnn-nnn where ‘n’ represents a digit. Write a function valid_sn that takes a string as a parameter and returns True if the serial number is valid and False otherwise. For example, the input "SN/5467-231" returns True "SN5467-231" returns False "SN/5467-2231" returns False Using this function, write and test another function valid-sn-file that takes three file handles as input parameters. The first handle...

  • PYTHON Text file Seriesworld attached below and it is a list of teams that won from...

    PYTHON Text file Seriesworld attached below and it is a list of teams that won from 1903-2018. First time mentioned won in 1903 and last team mentioned won in 2018. World series wasn’t played in 1904 and 1994 and the file has entries that shows it. Write a python program which will read this file and create 2 dictionaries. The first key of the dictionary should show name of the teams and each keys value that is associated is the...

  • Java : I keep getting something wrong in my loadfile method, how do I fix my...

    Java : I keep getting something wrong in my loadfile method, how do I fix my code? Thank you. here is what is contained in the text file (WorldSeriesWinners.txt) 112 1903 Boston Americans 1904 No World Series 1905 New York Giants 1906 Chicago White Sox 1907 Chicago Cubs 1908 Chicago Cubs 1909 Pittsburgh Pirates 1910 Philadelphia Athletics 1911 Philadelphia Athletics 1912 Boston Red Sox 1913 Philadelphia Athletics 1914 Boston Braves 1915 Boston Red Sox 1916 Boston Red Sox 1917 Chicago...

  • write a SQL statements to perform the following: Select all years a World Series game was...

    write a SQL statements to perform the following: Select all years a World Series game was played Select all losing teams that played in a World Series game Select all winning and losing teams that played in a World Series game Select all cities of a winning or losing team that played in a World Series game Select all winning and losing teams that played in a World Series game, and provide aliases of "Winning Team" and "Losing Team" Select...

  • To be done on excel: Team Revenue ($ millions) Value ($ millions) Arizona Diamondbacks 195 584...

    To be done on excel: Team Revenue ($ millions) Value ($ millions) Arizona Diamondbacks 195 584 Atlanta Braves 225 629 Baltimore Orioles 206 618 Boston Red Sox 336 1,312 Chicago Cubs 274 1,000 Chicago White Sox 216 692 Cincinnati Reds 202 546 Cleveland Indians 186 559 Colorado Rockies 199 537 Detroit Tigers 238 643 Houston Astros 196 626 Kansas City Royals 169 457 Los Angeles Angels of Anaheim 239 718 Los Angeles Dodgers 245 1,615 Miami Marlins 195 520 Milwaukee...

  • 3) American League baseball teams play their games with the designated hitter rule, meaning that pitchers...

    3) American League baseball teams play their games with the designated hitter rule, meaning that pitchers do not bat. The league believes that replacing the pitcher, typically a weak hitter, with another player in the batting order produces more runs. Using a significance level of a = 0.05, determine if the average number of runs is higher for the American League Following are the average number of runs scored by each team in the 2016 season: American League National League...

  • Tampa Texas Toront 4-30 In 2012, the total payroll for the New York Yankees was almost...

    Tampa Texas Toront 4-30 In 2012, the total payroll for the New York Yankees was almost $200 million, while the total payroll for the Oakland Athletics (a team known for using baseball analytics or sabermetrics) was about $55 million, less than one-third of the Yankees' payroll. In the following table, you will see the payrolls (in millions) and the total number of victories for the baseball teams in the American League in the 2012 season. Develop a regression model to...

  • 5. You and your friends want to select a few Major League Baseball (MLB) teams to...

    5. You and your friends want to select a few Major League Baseball (MLB) teams to create a fantasy league. You write a code to select teams at random. (Divisions and teams are listed below.) (12pts.) AL East AL West AL Central HAstros Orioles White Sox B C Red Sox Indians Angels Tigers Yankees Athletics KC Rays Royals Mariners T Twins Blue Jays Rangers NL East NL Central NL West C A Diamondbacks Braves Cubs Marlins Reds Rockies Mets Brewers...

  • Team League Wins ERA BA HR SB Errors Built Size Attendance Payroll Pittsburgh Pirates NL 57...

    Team League Wins ERA BA HR SB Errors Built Size Attendance Payroll Pittsburgh Pirates NL 57 5.00 0.242 126 87 127 2001 38496 1.61 34.9 San Diego Padres NL 90 3.39 0.246 132 124 72 2004 42445 2.13 37.8 Oakland Athletics AL 81 3.56 0.256 109 156 99 1966 34077 1.42 51.7 Texas Rangers AL 90 3.93 0.276 162 123 105 1994 49115 2.51 55.3 Florida Marlins NL 80 4.08 0.254 152 92 123 1987 36331 1.54 55.6 Arizona Diamondbacks...

  • The average number of minutes Americans commute to work is 27.7 minutes (Sterling's Best Places, April...

    The average number of minutes Americans commute to work is 27.7 minutes (Sterling's Best Places, April 13, 2012). The average commute time in minutes for 48 cities are as follows: Click on the datafile logo to reference the data. DATA file Albuquerque Atlanta Austin Baltimore Boston Charlotte Chicago Cincinnati Cleveland Columbus Dallas Denver Detroit El Paso Fresno Indianapolis 23.3 28.3 24.6 32.1 31.7 25.8 38.1 24.9 26.8 23.4 28.5 28.1 29.3 24.4 23.0 24.8 Jacksonville Kansas City Las Vegas Little...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
Active Questions
ADVERTISEMENT