Question
import java.util.*; import java.io.*; class WorldSeries { // main method public static void main(String args[]) throws IOException { // scanner class object to read input
import java.util.*;
import java.io.*;
class WorldSeries
{
// main method
public static void main(String args[]) throws IOException { // scanner class object to read input file Scanner reader = new Scanner(new File("WorldSeriesWinners.txt")); // worldseries class objects to call other methods WorldSeries obj = new WorldSeries(); // scanner class object for reading user input Scanner ipReader = new Scanner(System.in); // string array to hold series winners read from input file String[] winners = new String[114]; int i = 113; // loop till all records are read from input file while (reader.hasNext()) { // reading each line and placing in array winners[i] = reader.nextLine(); // decresing value of i i = i - 1; } // closing file reader.close(); // for funFact array - start String[] funFacts = new String[10]; reader = new Scanner(new File("FunFacts.txt")); i=0; while (reader.hasNext()) { // reading each line and placing in array funFacts[i] = reader.nextLine(); // increasing value of i i++; } reader.close(); // for funFact array - end int option, year; String team; // loop till user want to exit while (true) { // for funfact array - start System.out.println(obj.getFunFact(funFacts)); // for funfact array - stop // displaying menu obj.menu(); // reading choice System.out.print(" Enter choice: "); option = ipReader.nextInt();//geting user input as a option // calling functions switch (option) { case 1: obj.displayWinners(winners);//calling this method to display winner based on the input file break; case 2: obj.displayWinnersByYear(winners);//calling this method to display winner based on year break; case 3: System.out.print(" Enter a team name: "); ipReader.nextLine(); team = ipReader.nextLine();//geting user input as a team name obj.findWinner(winners, team);//Calling this method to find winner based on the user input break; case 4: System.out.print(" Enter a team name: "); ipReader.nextLine(); team = ipReader.nextLine();//geting user input as a team name obj.findYears(winners, team);//calling this method to find winner based on the user input break; case 5: System.out.print(" Enter a year: "); year = ipReader.nextInt();//getting user input as a year obj.winner(winners, year);//calling this method to find winner based on the user input break; case 6: return;//breaking while loop default: System.out.println(" Invalid option!!! "); } } }
/*
* @param - none
* @return - none
* The method prints menu for user selection and a fun fact
*/
void menu() { System.out.println(" Main Menu 1 - Show all winners - descending years 2 - Show all winners - ascending years 3 - Show how many times a particular team has won 4 - Show the years that a particular team has won 5 - Show the winner for a particular year 6 - Exit "); }
/*
* @param string[] winners - array of winners
* @return - none
* this method displays world series winners in descending order
*/
void displayWinners(String[] winners) { System.out.println(" World Series Winners: "); //iterating array from the last index to first index to get each winner based on the year for (int j = winners.length - 1; j >= 0; j--) { System.out.printf(" %-8d %-30s ", j + 1903, winners[j]); } }
/*
* @param string[] winners - array of winners
* @return - none
* This method displays world series winners in ascending order
*/
void displayWinnersByYear(String[] winners) { System.out.println(" World Series Winners: "); //iterating array from the first index to last index to get each winner for (int j = 0; j < winners.length; j++) { System.out.printf(" %-8d %-30s ", j + 1903, winners[j]); } }
/*
* @param string[] winners - array of winners
* @param string team - team name provided by user
* @return - none
* This method displays number of times a team has won
*/
void findWinner(String[] winners, String team) { int cnt = 0; //iterating array from the first index to last index to get each winner for (int j = 0; j < winners.length; j++) { // checking If any team name matches with array elements if (winners[j].equalsIgnoreCase(team)) cnt = cnt + 1;//if match increment the count } // checking count value, if not equal 0 then matching found else no match if (cnt == 0) { System.out.println(" Sorry!!! the team has to win at least once "); } else { System.out.printf(" Team %s have won the series %d times ", team, cnt); } }
/*
* @param string[] winners - array of winners
* @param string team - team name provided by user
* @return - none
* This method displays number of times a team has won
*/
void findYears(String[] winners, String team) { int found = 0; //iterating array from the last index to first index to get each winner for (int j = winners.length - 1; j >= 0; j--) { // checking If any team name matches with array elements if (winners[j].equalsIgnoreCase(team)) { //if match increment the count printing values System.out.println(1903 + j); found = 1; } } // checking found value if equal to 0 then matching not found if (found == 0) { System.out.println(" Sorry!!! the team has to win at least once "); } }
/*
* @param string[] winners - array of winners
* @param int year - year provided by user
* @return - none
* this method displays number of times a team has won
*/
void winner(String[] winners, int year) { // checking year if (year < 1903 || year > 2016) { System.out.printf(" The World Series winner for the year %d has yet to be determined! ",year); } else { // if there is a match increment the count if (winners[year - 1903].equalsIgnoreCase("NO GAME PLAYED")) { // printing values System.out.printf(" The World Series was not played in %d ", year); } else { System.out.printf(" The winner of the World Series in year %d was the %s ", year, winners[year - 1903]); } } }
/*
* @param no parameters
* @return - none
* this method displays random fun facts each time the menu is called
*/
String getFunFact(String funFact[]) { int rnd = new Random().nextInt(10); // 10 is the length of the funfact array return funFact[rnd]; // returning the random value from the array }//end module
}//end class
everything works any suggestions to the comments that are written ?
updates errors
just trying to perfect this program
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started