Question

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 White Sox

1918 Boston Red Sox

1919 Cincinnati Reds

1920 Cleveland Indians

1921 New York Giants

1922 New York Giants

1923 New York Yankees

1924 Washington Senators

1925 Pittsburgh Pirates

1926 St. Louis Cardinals

1927 New York Yankees

1928 New York Yankees

1929 Philadelphia Athletics

1930 Philadelphia Athletics

1931 St. Louis Cardinals

1932 New York Yankees

1933 New York Giants

1934 St. Louis Cardinals

1935 Detroit Tigers

1936 New York Yankees

1937 New York Yankees

1938 New York Yankees

1939 New York Yankees

1940 Cincinnati Reds

1941 New York Yankees

1942 St. Louis Cardinals

1943 New York Yankees

1944 St. Louis Cardinals

1945 Detroit Tigers

1946 St. Louis Cardinals

1947 New York Yankees

1948 Cleveland Indians

1949 New York Yankees

1950 New York Yankees

1951 New York Yankees

1952 New York Yankees

1953 New York Yankees

1954 New York Giants

1955 Brooklyn Dodgers

1956 New York Yankees

1957 Milwaukee Braves

1958 New York Yankees

1959 Los Angeles Dodgers

1960 Pittsburgh Pirates

1961 New York Yankees

1962 New York Yankees

1963 Los Angeles Dodgers

1964 St. Louis Cardinals

1965 Los Angeles Dodgers

1966 Baltimore Orioles

1967 St. Louis Cardinals

1968 Detroit Tigers

1969 New York Mets

1970 Baltimore Orioles

1971 Pittsburgh Pirates

1972 Oakland Athletics

1973 Oakland Athletics

1974 Oakland Athletics

1975 Cincinnati Reds

1976 Cincinnati Reds

1977 New York Yankees

1978 New York Yankees

1979 Pittsburgh Pirates

1980 Philadelphia Phillies

1981 Los Angeles Dodgers

1982 St. Louis Cardinals

1983 Baltimore Orioles

1984 Detroit Tigers

1985 Kansas City Royals

1986 New York Mets

1987 Minnesota Twins

1988 Los Angeles Dodgers

1989 Oakland Athletics

1990 Cincinnati Reds

1991 Minnesota Twins

1992 Toronto Blue Jays

1993 Toronto Blue Jays

1994 No World Series

1995 Atlanta Braves

1996 New York Yankees

1997 Florida Marlins

1998 New York Yankees

1999 New York Yankees

2000 New York Yankees

2001 Arizona Diamondbacks

2002 Anaheim Angels

2003 Florida Marlins

2004 Boston Red Sox

2005 Chicago White Sox

2006 St. Louis Cardinals

2007 Boston Red Sox

2008 Philadelphia Phillies

2009 New York Yankees

2010 San Francisco Giants

2011 St. Louis Cardinals

2012 San Francisco Giants

2013 Boston Red Sox

2014 San Francisco Giants

Public Class Winner {

private String team;

       private int year;

      

       private Winner()

       {

           /*

           * Make default constructor private so programers who use this

           * class are forced to use the constructor that requires them

           * to provide both the team name and year

           */

          

       }

      

       public Winner(String team, int year)

       {

           this.team = team;

           this.year = year;

       }

      

       public String getTeam() {

           return team;

       }

       public int getYear() {

           return year;

       }

      

      

   }

import java.io.File;

import java.util.Scanner;

public class MainClass {

public static Winner [] listOfWinners;

public static void loadFromFile()

{

try{

//Create instance of Scanner and provide instance of File pointing to the txt file

Scanner input = new Scanner(new File("WorldSeriesWinners.txt"));

//Get the number of teams

int years = input.nextInt();

input.nextLine();//move to the next line

//Create the array

listOfWinners = new Winner[years];

//for every year in the text file

for(int index = 0; index

{

//Get the year

int year = input.nextInt();

input.skip(" ");

//Get the team

String team = input.nextLine();

//Create an instance of Winner and add it to the next spot in the array

listOfWinners[index] = new Winner(team,year);

}

}catch(Exception e)

{

System.out.println("Hey Meatbag, I'm Bender, and something wrong in the loadFromFile method happened!");

System.out.println(e.toString());

System.exit(0);

}

}

public static void sortByTeamName()

{

for (int i = 0; i < listOfWinners.length-1; i++)

{

int minimumIndex = i;

for (int j = i+1; j < listOfWinners.length; j++)

{ if (listOfWinners[j].getTeam().compareTo(listOfWinners[minimumIndex].getTeam())==-1)

{ minimumIndex = j;

}

}

Winner temp = listOfWinners[minimumIndex];

listOfWinners[minimumIndex] = listOfWinners[i];

listOfWinners[i] = temp;

}

}

public static void sortByYear()

{

for (int i = 0; i < listOfWinners.length-1; i++)

{

int minimumIndex = i;

for (int j = i+1; j < listOfWinners.length; j++)

{

if (listOfWinners[j].getYear()

{ minimumIndex = j;

}

}

Winner temp = listOfWinners[minimumIndex];

listOfWinners[minimumIndex] = listOfWinners[i];

listOfWinners[i] = temp;

}

}

public static void printArray()

{

//Print the array

for(int index=0; index

{

System.out.println(listOfWinners[index].getYear()+" Winners " + listOfWinners[index].getTeam());

}

}

public static void searchForWinnerByYear(int year)

{

String team="no one";

for(int i=0;i

{

if(listOfWinners[i].getYear()==year)

{

team=listOfWinners[i].getTeam();

}

}

System.out.println("team won was :"+team);

}

public static void searchForYearsATeamWon(String team)

{

for(int i=0;i

{

if(listOfWinners[i].getTeam().equals(team))

{

System.out.println("year :"+listOfWinners[i].getYear());

}

}

}

public static void main(String[] args) {

int choice;

Scanner keyboard = new Scanner(System.in);

System.out.println("World Series Program");

//Load text file

loadFromFile();

do{

System.out.println("1.........Print out the Winner List sorted by team name");

System.out.println("2.........Print out the Winner List sorted by years");

System.out.println("3.........Print out the Winner of a particular year");

System.out.println("4.........Print out the years a particular team won");

System.out.println("5.........Exit the Program");

System.out.println("Which Choice Would You Like?");

choice = keyboard.nextInt();

switch(choice)

{

case 1:

//Option 1, sort array by name and print out.

sortByTeamName();

printArray();

break;

case 2:

//Option 2, sort array by year and print out.

sortByYear();

printArray();

break;

case 3:

//Option 3 Search for winner by year

System.out.println("What year would you like to find the winner?");

int year = keyboard.nextInt();

searchForWinnerByYear(year);

break;

case 4:

//Option 4 Search for years a team won

System.out.println("What team would you like to check for years won?");

keyboard.nextLine();

String team = keyboard.nextLine();

searchForYearsATeamWon(team);

break;

case 5://Exit

break;

default:

System.out.println("Invalid Choice");

}

}while(choice != 5);

System.out.println("Goodbye!");

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//MainClass.java

import java.io.File;

import java.util.Arrays;

import java.util.Scanner;

public class MainClass {

public static Winner [] listOfWinners;

public static void loadFromFile()

{

try{

Scanner input = new Scanner(new File("WorldSeriesWinners.txt"));

//Get the number of teams

int years = input.nextInt();

input.nextLine();//move to the next line

//Create the array

listOfWinners = new Winner[years];

//for every year in the textfile

for(int index = 0; index<years; index++)

{

//Get the year

int year = input.nextInt();

input.skip(" ");

//Get the team

String team = input.nextLine();

//Create an instance of Winner and add it to the next spot in the array

listOfWinners[index] = new Winner(team,year);

}

for(int i=0; i < listOfWinners.length; i++)

{

//System.out.println(listOfWinners[i].getYear());

}

}catch(Exception e)

{

//System.out.println("Hey Meatbag, I'm Bender, and something wrong in the loadFromFile method happened!");

System.out.println(e.toString());

System.out.println(e.getMessage());

System.exit(0);

}

}

public static void sortByTeamName()

{

// store current i

Winner temp;

// store the lowest value

Winner min;

//Print the array

for(int i=0; i < listOfWinners.length; i++)

{

// select the first array value

min = listOfWinners[i];

for(int j=i+1; j < listOfWinners.length; j++)

{

// if value of the j is lower than the min make j the new min

if(listOfWinners[j].getTeam().compareTo(min.getTeam()) > 0)

{

// store new minmum

min= listOfWinners[j];

// hold i

temp = listOfWinners[i];

// make i the new min

listOfWinners[i] = min;

// j is the last value

listOfWinners[j] = temp;

}// end of if

} // end of for loop

} // end of for loop

}

public static void sortByYear()

{

Winner temp;

Winner min;

for(int i=0; i < listOfWinners.length-1; i++)

{

min = listOfWinners[i];

for(int j=i+1; j < listOfWinners.length; j++)

{

if(listOfWinners[j].getYear() < min.getYear())

{

min = listOfWinners[j];

temp = listOfWinners[i];

listOfWinners[i] = min;

listOfWinners[j] = temp;

} // end of if

} // end of for loop

} // end of for loop

}

public static void printArray()

{

//Print the array

for(int index=0; index<listOfWinners.length; index++)

{

System.out.println(listOfWinners[index].getYear()+" Winners " + listOfWinners[index].getTeam());

}

}

public static void searchForWinnerByYear(int year)

{

boolean match = false;

int check = 0;

while(match != true)

{

for(int i=0; i < listOfWinners.length; i++)

{

// check to see if the numbers match

if(listOfWinners[i].getYear() == year)

{

match = true;

System.out.println("Year:" + listOfWinners[i].getYear() + " Winner:" + listOfWinners[i].getTeam());

}

}// end of for loop

// if number does not exit

if(check == 0)

{

System.out.println("Invalid year");

match = true;

}

}// end of while loop

}

public static void searchForYearsATeamWon(String team)

{

boolean match = false;

int check = 0;

while(match != true)

{

for(int i=0; i < listOfWinners.length; i++)

{

if(listOfWinners[i].getTeam().compareTo(team) == 0)

{

match = true;

System.out.println("Year:" + listOfWinners[i].getYear() + " Winner:" + listOfWinners[i].getTeam());

}

}// end of loop

if(check == 0)

{

System.out.println("Invalid Team");

match = true;

}

}// end of loop

}

public static void main(String[] args) {

int choice;

Scanner keyboard = new Scanner(System.in);

System.out.println("World Series Program");

//Load textfile

loadFromFile();

do{

System.out.println("1.........Print out the Winner List sorted by team name");

System.out.println("2.........Print out the Winner List sorted by years");

System.out.println("3.........Print out the Winner of a particular year");

System.out.println("4.........Print out the years a particular team won");

System.out.println("5.........Exit the Program");

System.out.println("Which Choice Would You Like?");

choice = keyboard.nextInt();

switch(choice)

{

case 1:

//Option 1, sort array by name and print out.

sortByTeamName();

printArray();

break;

case 2:

//Option 2, sort array by year and print out.

sortByYear();

printArray();

break;

case 3:

//Option 3 Search for winner by year

System.out.println("What year would you like to find the winner?");

int year = keyboard.nextInt();

searchForWinnerByYear(year);

break;

case 4:

//Option 4 Search for years a team won

System.out.println("What team would you like to check for years won?");

keyboard.nextLine();

String team = keyboard.nextLine();

searchForYearsATeamWon(team);

break;

case 5://Exit

break;

default:

System.out.println("Invalid Choice");

}

}while(choice != 5);

System.out.println("Goodbye!");

}

}

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

//Winner.java

public class Winner {

private String team;

private int year;

private Winner()

{

}

public Winner(String team, int year)

{

this.team = team;

this.year = year;

}

public String getTeam() {

return team;

}

public int getYear() {

return year;

}

}

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

sample output

,M Main-04-Template.jav X 고 gty/src/Main_04_Template.java *MainClass.java X id loadFromFile() U winner.java EmployeeParameter

Add a comment
Know the answer?
Add Answer to:
Java : I keep getting something wrong in my loadfile method, how do I fix my...
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 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...

  • 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...

  • 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...

  • I keep getting this error and I do not know how to fix it. this is...

    I keep getting this error and I do not know how to fix it. this is the code I've been working on: please help me import java.util.*; public class TelephoneNumber { public static void main(String[] args) {String number; int i=0,j=0; char c; Scanner in=new Scanner (System.in); System.out.println("Enter the phone number: "); number=in.nextLine(); number=number.toUpperCase(); c=number.charAt(i); while(c!='\n'&&j<=7) {switch(c) {case 'A': case 'B': case 'C': System.out.print("2"); j++; break; case 'D': case 'E': case 'F': System.out.print("3"); j++; break; case 'G': case 'H': case'I': System.out.print("4");...

  • 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...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • [Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what...

    [Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what is wrong with my code. Here'e the problem: Write a class SemiCircle that represents the northern half of a circle in 2D space. A SemiCircle has center coordinates and a radius. Define a constructor: public SemiCircle(int centerX, int centerY, int theRadius) Implement the following methods: public boolean contains(int otherX, int otherY) returns true if the point given by the coordinates is inside the SemiCircle....

  • How do I separate the method into different class? I try separate the testing and method...

    How do I separate the method into different class? I try separate the testing and method class into 2 different class. And when I test it, it said that the method is undefined. And it ask me to create the method in the main class. I don't know what is the problem. This is the access to the code https://repl.it/@Teptaikorn/test Thank you very much MazeSolver.java MazeTerster.... TwoDim AutoA... testfile.txt Main.java x > 0 N Binary TreeN... X LinkedBinary... Maze.java 1...

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
ADVERTISEMENT
ADVERTISEMENT