Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Brandon Silver
- // Section 004
- // CS 1113
- // 2011-12-09
- //
- // Reads in football statistics from supplied input, prints
- // out the input, sorts the input, and then prints it back out.
- // Then, searches the sorted data for the supplied statistics.
- import java.util.Scanner;
- public class Football2 {
- public static void main(String[] args)
- {
- // declare final values
- final int ARR_SIZE = 100;
- final String SENT = "NoSuchTeam";
- final int TEAM_LENGTH = 4;
- // declare football team object array
- FootballTeam[] footballArr = new FootballTeam[ARR_SIZE];
- // initialize footbal teams to fill footballArr
- for (int i = 0; i < ARR_SIZE; i++)
- footballArr[i] = new FootballTeam();
- // declare scanner
- Scanner scan = new Scanner(System.in);
- // declare all other variables
- String titleLine = "";
- int numRecords = 0;
- String sTeamKey = "";
- int iYardsNumKey = 0;
- int elementPos = 0;
- FootballTeam footballTeamKey = new FootballTeam();
- // Read in first line (title) into a variable for printing
- // (placed here b/c it doesn't really belong in readDatabase method)
- if (scan.hasNext())
- titleLine = scan.nextLine();
- else
- {
- System.out.println("ERROR: No input detected. Quitting program.");
- System.exit(0);
- }
- // Print titleLine
- System.out.println(titleLine);
- // call readDatabase
- numRecords = readDatabase(scan, ARR_SIZE, SENT, footballArr);
- // call printDatabase
- printDatabase(numRecords, footballArr);
- System.out.println("Sort the database.");
- // call sortDatabase (broken, I have no idea how to do this)
- sortDatabase(numRecords, footballArr);
- // print the newly sorted database (except it ain't sorted, see above)
- printDatabase(numRecords, footballArr);
- // Defensively read in the team
- if (scan.hasNext())
- sTeamKey = scan.next();
- // enter search sentinel loop (assuming it starts after first sentinel)
- while (!sTeamKey.equals(SENT))
- {
- // Defensively read in the yards
- if (scan.hasNextInt())
- iYardsNumKey = scan.nextInt();
- else
- {
- System.out.println("ERROR: Expecting an int. Quitting program.");
- System.exit(0);
- }
- // output search query
- System.out.println("\nIs there a team with an abbreviation = "
- + sTeamKey + "\n and total yards = " + iYardsNumKey
- + " in the database, \n and if so, what is its number of wins.");
- // load in temporary values into key FootballTeam object
- footballTeamKey.setName(sTeamKey);
- footballTeamKey.setYards(iYardsNumKey);
- elementPos = seqSearch(numRecords, footballArr, footballTeamKey);
- // output search results
- if (elementPos == -1)
- System.out.println("No, there is no such team in the database.");
- else
- {
- System.out.println("Yes. The number of wins is "
- + footballArr[elementPos].getWins() + ".");
- }
- // read in the next team name to look for
- if (scan.hasNext())
- sTeamKey = scan.next();
- else
- {
- System.out.println("ERROR: No input detected. Quitting Program.");
- System.exit(0);
- }
- } // end search sentinel loop
- } // end main method
- public static int readDatabase(Scanner scan, int ARR_SIZE, String nameSentinel,
- FootballTeam[] football)
- {
- // declare variable to return
- int numRecords = 0;
- // declare temporary container variables
- String sTeamName = "";
- int iAttemptsNum = 0;
- int iYards = 0;
- int iWin = 0;
- int iLose = 0;
- // Defensively read in team name
- if (scan.hasNext())
- {
- sTeamName = scan.next();
- }
- else
- {
- System.out.println("ERROR: Invalid input. Quitting program.");
- System.exit(0);
- }
- while (!sTeamName.equals(nameSentinel))
- {
- // attempts
- if (scan.hasNextInt())
- {
- iAttemptsNum = scan.nextInt();
- }
- else
- {
- System.out.println("ERROR: Invalid input. Quitting program.");
- System.exit(0);
- }
- // yards
- if (scan.hasNext())
- {
- iYards = scan.nextInt();
- }
- else
- {
- System.out.println("ERROR: Invalid input. Quitting program.");
- System.exit(0);
- }
- // wins
- if (scan.hasNextInt())
- {
- iWin = scan.nextInt();
- }
- else
- {
- System.out.println("ERROR: Invalid input. Quitting program.");
- System.exit(0);
- }
- // loses
- if (scan.hasNextInt())
- {
- iLose = scan.nextInt();
- }
- else
- {
- System.out.println("ERROR: Invalid input. Quitting Program.");
- System.exit(0);
- }
- // load the holder variables into the specific FootballTeam object
- // in the FootballTeam object array.
- // also verify that there is enough space in the array.
- if (numRecords < ARR_SIZE - 1)
- {
- football[numRecords].setName(sTeamName);
- football[numRecords].setAttempts(iAttemptsNum);
- football[numRecords].setYards(iYards);
- football[numRecords].setWins(iWin);
- football[numRecords].setLosses(iLose);
- }
- else
- {
- System.out.println("ERROR: There is not enough space in the"
- + " array. Quitting program.");
- System.exit(0);
- }
- // increment the number of records
- numRecords++;
- // Defensively read in team name
- if (scan.hasNext())
- {
- sTeamName = scan.next();
- }
- else
- {
- System.out.println("ERROR: Invalid input. Quitting program.");
- System.exit(0);
- }
- } // end data input sentinel loop
- return numRecords;
- }
- public static void printDatabase(int numRecords, FootballTeam[] footballArr)
- {
- System.out.println("\nPrint the database:");
- // begin output loop
- for (int i = 0; i < numRecords; i++)
- {
- // add spaces to names as needed
- // while (footballArr[i].getName().length() < 4)
- // {
- // footballArr[i].setName((footballArr[i].getName() + " "));
- // }
- System.out.println(footballArr[i].toString());
- }
- } // end printDatabase method
- public static void sortDatabase(int numRecords, FootballTeam[] footballArr)
- {
- int counter = 0;
- String tempStr = "";
- int tempInt = 0;
- FootballTeam tempTeam = new FootballTeam();
- for (int i = 1; i < numRecords; i++)
- {
- counter = i - 1;
- for (int j = i; j < numRecords; j++)
- {
- if(footballArr[j].compareTo(footballArr[counter]) < 0)
- counter = j;
- else if (footballArr[j].compareTo(footballArr[counter]) == 0)
- {
- counter = i + 1;
- }
- }
- tempTeam = footballArr[i-1];
- footballArr[i-1] = footballArr[counter];
- footballArr[counter] = tempTeam;
- }
- } // end sortDatabase method
- public static int seqSearch(int numRecords, FootballTeam[] footballArr,
- FootballTeam footballTeamKey)
- {
- return 0;
- } // end seqSearch method
- } // end Football2 class
Add Comment
Please, Sign In to add comment