r/programminghelp • u/No_Pizza4761 • Feb 12 '21
Java World Series Champions assignment
Hey so I was wondering if someone can help me understand what I"m doing wrong with this Java program assignment "World Series Champions"
so basically I need to write a program that lets the user enter a team name and indicate how many times they won the World Series through the Time Period 1903-2009.
I got everything else down except when it finds a team it says that team is not on the list so need help understanding what I'm doing wrong here so I can fix it
package com.WorldSeries;
// Solution to Spring 2021 COSC-1337-004 Program #2: World Series Problem
// Given the number of games in a series, and the probability of a team
// winning an individual game, this program will calculate the probability
// of the team winning the series.
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
/**
* This is a program for Assignment 2: World Series Champions of Spring 2021 COSC-1337-004
*/
public class Main {
public static int count = 0;
public static String output = "";
public static String input = "";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final String FILENAME = "WorldSeriesWinners.txt";
ArrayList<String> data = new ArrayList<String>();
// Read the data file
int reply = JOptionPane.showConfirmDialog(null,
"Do you want to use the default file " + FILENAME + "?",
"Do you want to use the default file?",
JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.NO_OPTION)
System.exit(0);
else {
// Read the data from file.
try {
data = readData(FILENAME);
} catch (IOException e) {
}
}
// Sequential search the String array and print the result
reply = JOptionPane.YES_OPTION;
while (reply == JOptionPane.YES_OPTION) {
count = 0;
output = "";
input = JOptionPane.showInputDialog(null,
"What team do you want stats on?");
if ((input != null) && (input.length() > 0)) {
boolean found = sequentialSearch(data, input);
if (found == true) {
JOptionPane.showMessageDialog(null, output);
} else {
JOptionPane.showMessageDialog(null,
"Can't find the team in the list.");
}
}
reply = JOptionPane.showConfirmDialog(null,
"Do you want to search for another team?",
"Do you want to search for another team?",
JOptionPane.YES_NO_OPTION);
}
System.exit(0);
}
/**
* The readData method reads the data from a text file 'filename' and save
* Strings in an ArrayList.
* u/param filename The file name of the text file to be read.
* u/return The ArrayList contains all Strings from the text file.
* u/throws IOException
*/
public static ArrayList<String> readData(String filename) throws IOException {
ArrayList<String> data = new ArrayList<String>();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
data.add(inputFile.nextLine());
}
inputFile.close();
return data;
}
/**
* The sequentialSearch method sequential search the String 'name' in the
* ArrayList 'array'.
* u/param array The ArrayList to search from.
* u/param name The String to search.
* u/return The boolean if String 'name' is found in ArrayList 'array'.
*/
public static boolean sequentialSearch(ArrayList<String> array, String name) {
final int STARTYEAR = 1903;
int year;
boolean found = false;
for (int index = 0; index < array.size(); index++) {
if (array.get(index).compareTo(name) == 0) {
count ++;
year = STARTYEAR + index;
if ((year >= 1904) && (year < 1993))
year = year + 1;
else if (year >= 1993)
year = year + 2;
output = output + Integer.toString(year) + ", ";
found = true;
}
}
output = "The " + input + " have won the World Series in the years:\n" +
output + "\nand won the World Series " + count + " times.";
return found;
}
}
2
1
Feb 13 '21
The first thing you're doing wrong is not reading the sidebar and following Rule #2 ;) Please cleanup your code! Hopefully then someone can give you some assistance.
2
u/EnvironmentalCow3040 Feb 13 '21