/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package fourteenthfall16; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; /** * * @author jcmtiernan */ public class FourteenthFall16 { final static int TEAMNUM = 0; // column 0 is the team number final static int TEAMRP = 1; // column 1 is the team RP final static int TEAMQP = 2; // column 2 is the team QP final static int TEAMRANK = 3; final static int MAXTEAMS = 100; /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input = new Scanner(System.in); File inputData = new File("TeamsRPQP13"); Scanner inFile; boolean fileFound = true; try { inFile = new Scanner(inputData); } catch (FileNotFoundException fnf) { inFile = new Scanner(System.in); System.out.println("Can't find input file"); fileFound = false; } final int TEAMNAME = 1; // column 1 is the team name final int TEAMSTATUS = 0; // column 0 is the team status (rookie or veteran) String[][] teamNames =new String[MAXTEAMS][2]; int colData = 4; int[][] teamsData = new int[MAXTEAMS][colData]; boolean invalidData = false; int i = 0; int numberOfTeams = 0; // Read in team names and numbers from a file // Going to not read data if there is no file if (fileFound) { //System.out.println("In found file"); numberOfTeams= storeFileData(inFile, MAXTEAMS, teamsData, teamNames); } System.out.println(numberOfTeams+" teams found in file."); // Print matching team numbers and names // *** print team RP and QP System.out.println(); System.out.println(); i = 0; for( int ndex = 0; ndex < numberOfTeams; ndex++ ) // ( variable declaration : arrayName ) { if (teamsData[ndex][TEAMNUM] > 0) // teams[index] is replaced currTeam { printTeamBanner2D(teamsData[ndex],teamNames[ndex]); } i++; } if (numberOfTeams < 1) { System.out.println("No teams found in file"); } System.out.println(); System.out.println("Before sorting: "); printTeamTable(teamsData, teamNames,"Count"); // *** Find the highest ranked team and print it int highestIndex = findHighestTeam(teamsData); System.out.println("The highest ranked team is team "+teamsData[highestIndex][TEAMNUM]); System.out.println(); // bubble sort bubbleSortTeamNums (teamsData, teamNames, colData, TEAMNUM); System.out.println(); System.out.println("After sorting by team number: "); printTeamTable(teamsData, teamNames,"Order"); System.out.println(); bubbleSortTeamRank(teamsData, teamNames, colData, TEAMNUM); System.out.println(); System.out.println("After sorting by team ranking: "); printTeamTable(teamsData, teamNames,"Rank"); System.out.println(); System.out.println("Using .toString"); for (int k= 0; k < teamsData.length;k++) { if (teamsData[k][TEAMNUM] != 0) System.out.println(Arrays.toString(teamsData[k]) ); } // use updates file // read in team number, an RP, and a QP and add the // RP and QP values to the current values for that team if (fileFound) { updateTeamList(teamsData); } System.out.println(); // bubble sort mergeSortTeamNums (teamsData, teamNames, 0, MAXTEAMS); System.out.println(); System.out.println("After updating and MERGE sorting by team number: "); printTeamTable(teamsData, teamNames,"Order"); System.out.println(); int searchedNumber = 0; searchedNumber = 6566; /* Scanner userInput = new Scanner(System.in); System.out.print("Enter a team number to find in the team list (or 0 for no team): "); if (userInput.hasNextInt()) { searchedNumber = userInput.nextInt(); } */ /* int foundIndex = findBinaryTeam(teamsData, searchedNumber); if (foundIndex >= 0){ System.out.println("The team "+searchedNumber+" is in the array at row index "+foundIndex); } else { System.out.println("The team "+searchedNumber+" is not in the array"); } System.out.println(); System.out.println(); */ //System.out.println(" Factorial of 7 is "+fac(7)); /* bubbleSortTeamRank(teamsData, teamNames, colData, TEAMNUM); System.out.println(); System.out.println("After sorting by team ranking after update: "); printTeamTable(teamsData, teamNames,"Rank"); // Removing data for a team 6566 bubbleSortTeamNums (teamsData, teamNames, colData, TEAMNUM); System.out.println(); System.out.println("After resorting by team number: "); printTeamTable(teamsData, teamNames,"Order"); System.out.println(); boolean success = true; int searchedNumber = 0; Scanner userInput = new Scanner(System.in); System.out.print("Enter a team number to remove from the team list (or 0 for no team): "); if (userInput.hasNextInt()) { searchedNumber = userInput.nextInt(); } success = removeTeam(teamsData, teamNames, searchedNumber); if (success) { System.out.println(); System.out.println("After removing team and resorting : "); printTeamTable(teamsData, teamNames,"Order"); } System.out.println(); System.out.print("Enter a team number to add to the team list (or 0 for no team): "); if (userInput.hasNextInt()) { searchedNumber = userInput.nextInt(); } success = addTeam(teamsData, teamNames, searchedNumber); if (success) { System.out.println(); System.out.println("After adding team and resorting : "); printTeamTable(teamsData, teamNames,"Order"); System.out.println(); } */ } public static int fac( int k) { if (k==0) { System.out.println("Fac of 0 is 1"); return 1; } System.out.println("Fac of "+k+" is fac("+k+"-1 * "+k); return fac(k - 1) * k; } public static void updateTeamList(int teamsData[][]) { int tnum = 0; int tRP = 0; int tQP = 0; int tRank = 0; boolean fileFound = true; boolean invalidData = false; Scanner inFile; File inputData = new File("TeamsRPQPupdates13"); try { inFile = new Scanner(inputData); System.out.println("Opening update file"); } catch (FileNotFoundException fnf) { inFile = new Scanner(System.in); System.out.println("Can't find updates file"); fileFound = false; } if (fileFound) { //System.out.println("Ready to read from updates file "); /* Read the data from the updates file and add it to the appropriate array values. Sort and print the updated data by team number order and then sort and print by rank */ String inputLine = ""; Scanner inLine = new Scanner(inputLine); String inWord = ""; boolean noQP = false; boolean noRank = false; while(inFile.hasNextLine()) { //System.out.println("In while loop"); inputLine = inFile.nextLine(); // read in one whole line invalidData = false; inLine = new Scanner(inputLine); // set up to read from line if (inLine.hasNextInt()) // is there an int at beginning of line? { //System.out.println("first line has next int"); tnum = inLine.nextInt(); // get team number inWord = inLine.next(); // get RP (using try catch as a demo) try // next check for int ranking points RP { tRP = Integer.parseInt(inWord); } catch (NumberFormatException nfe) { invalidData = true; // No RP value } if (inLine.hasNextInt() && !invalidData) // check for QP { tQP = inLine.nextInt(); } else if (!inLine.hasNextInt() && inLine.hasNext()) { invalidData = true; }// no int for qualifying points found else { noQP = true; // No QP value } if (inLine.hasNextInt() && !invalidData && !noQP) // check for QP { tRank = inLine.nextInt(); } else if (!inLine.hasNextInt() && inLine.hasNext()) { invalidData = true; }// no int for qualifying points found else // no int for qualifying points found { noRank = true; // No QP value } //System.out.println("Read "+tnum+" "+tRP+" "+tQP); // go through array to find matching team number boolean tGood = false; for (int i = 0; (i < teamsData.length); i++) { if ((teamsData[i][TEAMNUM] == tnum) && (invalidData == false)) { teamsData[i][TEAMRP] += tRP; tGood = true; //System.out.println("Added "+tRP+" RP and "+tQP+" QP for team "+tnum); } if (tGood && !noQP) { teamsData[i][TEAMQP] += tQP; } if (tGood && !noRank) { teamsData[i][TEAMRANK] = tRank; } } } else { invalidData = true; } } } } public static int findBinaryTeam(int[][] tD, int findTeam) { /* use Binary search to determine if a team is in the list and return it's location (index) if it is */ int findIndex = -1; int low = 0; int high = tD.length - 1; // list must be sorted first - so only works on sorted list // check middle of list // from http://www.cs.toronto.edu/~reid/search/bincode.html while(high >= low) { int middle = (low + high) / 2; if(tD[middle][TEAMNUM] == findTeam) { findIndex = middle; low = high + 1; } else if (tD[middle][TEAMNUM] < findTeam) { low = middle + 1; } else if (tD[middle][TEAMNUM] > findTeam) { high = middle - 1; } System.out.println("In Bin search, high is "+high+" and low is "+low); } return findIndex; } public static int findHighestTeam(int[][] tD) { /* Algorithm to find the highest ranked team by Rp then QP Declare variables for highRP, highQP, highIndex */ int highRP = -1; int highQP = -1; int highIndex = 0; final int TEAMNUM = 0; // column 0 is the team number final int TEAMRP = 1; // column 1 is the team RP final int TEAMQP = 2; // column 2 is the team QP /* Loop through the RP array */ // Linear search for (int i = 0; i < tD.length; i++) { if ((tD[i][TEAMRP] > highRP) || ((tD[i][TEAMRP] == highRP) && (tD[i][TEAMQP] > highQP))) { highRP = tD[i][TEAMRP]; highQP = tD[i][TEAMQP]; highIndex = i; } } return highIndex; } public static void printTeamBanner2D(int teams[],String[] teamName) { final int TEAMNUM = 0; // column 0 is the team number final int TEAMRP = 1; // column 1 is the team RP final int TEAMQP = 2; // column 2 is the team QP final int TEAMNAME = 1; final int TEAMSTATUS = 0; if ((teams[TEAMNUM] > 0) && (teams[TEAMNUM] < 20000)) { System.out.println(teamName[TEAMSTATUS] +" team "+teams[TEAMNUM] +", called "+teamName[TEAMNAME] +", has ranking points of "+ teams[TEAMRP] +" and qualifying points of "+teams[TEAMQP]); } } public static void printTeamTable(int teams[][],String[][] teamName,String title) { final int TEAMNUM = 0; // column 0 is the team number final int TEAMRP = 1; // column 1 is the team RP final int TEAMQP = 2; // column 2 is the team QP final int TEAMNAME = 1; final int TEAMSTATUS = 0; System.out.printf("%6s%10s%35s%8s%8s%10s\n",title,"Team #","Team Name","RP","QP","Status"); for (int i= 0; i 0) && (teams[i][TEAMNUM] < 20000)) { System.out.printf("%6d%10s%35s%8d%8d%10s\n",i+1,teams[i][TEAMNUM], teamName[i][TEAMNAME],teams[i][TEAMRP], teams[i][TEAMQP],teamName[i][TEAMSTATUS]); } } } // mergesort from www.cs.cmu.edu/~adamchik public static void mergeSortTeamNums (int[][] teamsData, String[][] teamNames, int left, int right) { if( left < right ) { int center = (left + right) / 2; mergeSortTeamNums(teamsData, teamNames, left, center); mergeSortTeamNums(teamsData, teamNames, center + 1, right); mergeTeamNums(teamsData, teamNames, left, center + 1, right); } } public static void mergeTeamNums (int[][] teamsData, String[][] teamNames, int left, int right, int rightEnd) { int colLength = teamsData[0].length; int colSLength = teamNames[0].length; int[][] tempTeamsData = new int[MAXTEAMS][colLength]; String[][] tempTeamNames = new String[MAXTEAMS][colSLength]; //System.out.println(); int leftEnd = right - 1; // arrays are left to leftEnd and right to rightEnd int k = left; int num = rightEnd - left + 1; //System.out.println("in merge - left is "+left+" and right is "+right); while(left <= leftEnd && right <= rightEnd && left >= 0 && right < MAXTEAMS) { //System.out.println("in merge - left is "+left+" and right is "+right); if(teamsData[left][TEAMNUM] < (teamsData[right][TEAMNUM])) { tempTeamsData[k] = teamsData[left]; tempTeamNames[k++] = teamNames[left++]; } else { tempTeamsData[k] = teamsData[right]; tempTeamNames[k++] = teamNames[right++]; } } while(left <= leftEnd && left >= 0) // Copy rest of first half { tempTeamsData[k] = teamsData[left]; tempTeamNames[k++] = teamNames[left++]; } while(right <= rightEnd && right < MAXTEAMS) // Copy rest of right half { tempTeamsData[k] = teamsData[right]; tempTeamNames[k++] = teamNames[right++]; } if (rightEnd >= MAXTEAMS) rightEnd = MAXTEAMS - 1; for(int i = 0; i < num && rightEnd >= 0; i++, rightEnd--) { teamsData[rightEnd] = tempTeamsData[rightEnd]; teamNames[rightEnd] = tempTeamNames[rightEnd]; } } public static void bubbleSortTeamNums (int[][] teamsData, String[][] teamNames, int colData, int TEAMNUM) { int[] tempArray = new int[colData]; String[] tempName = new String[2]; int swapCounter = 1; int comparisons = (teamsData.length); //passes System.out.println(); for (int zz = 0; (zz < teamsData.length) && (swapCounter > 0) ; zz++) { swapCounter = 0; comparisons--; //comparisons for (int index = 0; index < comparisons ; index++) { if ((teamsData[index][TEAMNUM] > teamsData[index+1][TEAMNUM]) && (teamsData[index+1][TEAMNUM] != 0)) { //System.out.println("teamsData[index][TEAMNUM] of "+ teamsData[index][TEAMNUM] // +" > teamsData[index+1][TEAMNUM] of "+teamsData[index+1][TEAMNUM]); tempArray = teamsData[index]; //tempName = teamNames[index]; tempName = teamNames[index]; teamsData[index] = teamsData[index+1]; teamNames[index] = teamNames[index+1]; teamsData[index+1] = tempArray; teamNames[index+1] = tempName; swapCounter++; //System.out.println("Swap made at index"+ index+" on pass "+zz); } } //System.out.println("On pass "+zz+" Swaps made :"+ swapCounter); } } public static void bubbleSortTeamNumsHi2Lo (int[][] teamsData, String[][] teamNames, int colData, int TEAMNUM) { int[] tempArray = new int[colData]; String[] tempName = new String[2]; int swapCounter = 1; int comparisons = (teamsData.length); //passes System.out.println(); for (int zz = 0; (zz < teamsData.length) && (swapCounter > 0) ; zz++) { swapCounter = 0; comparisons--; //comparisons for (int index = 0; index < comparisons ; index++) { if (teamsData[index][TEAMNUM] < teamsData[index+1][TEAMNUM]) { //System.out.println("teamsData[index][TEAMNUM] of "+ teamsData[index][TEAMNUM] // +" > teamsData[index+1][TEAMNUM] of "+teamsData[index+1][TEAMNUM]); tempArray = teamsData[index]; //tempName = teamNames[index]; tempName = teamNames[index]; teamsData[index] = teamsData[index+1]; teamNames[index] = teamNames[index+1]; teamsData[index+1] = tempArray; teamNames[index+1] = tempName; swapCounter++; //System.out.println("Swap made at index"+ index+" on pass "+zz); } } //System.out.println("On pass "+zz+" Swaps made :"+ swapCounter); } } public static void bubbleSortTeamRank (int[][] teamsData, String[][] teamNames, int colData, int TEAMNUM) { final int TEAMRP = 1; // column 1 is the team RP final int TEAMQP = 2; // column 2 is the team QP int[] tempArray = new int[colData]; String[] tempName = new String[2]; int swapCounter = 1; int comparisons = (teamsData.length); //passes System.out.println(); for (int zz = 0; (zz < teamsData.length) && (swapCounter > 0) ; zz++) { swapCounter = 0; comparisons--; //comparisons for (int index = 0; index < comparisons ; index++) { if (((teamsData[index][TEAMRP] < teamsData[index+1][TEAMRP]) || ((teamsData[index][TEAMRP] == teamsData[index+1][TEAMRP]) && (teamsData[index][TEAMQP] < teamsData[index+1][TEAMQP]))) && (teamsData[index+1][TEAMNUM] != 0)) { tempArray = teamsData[index]; tempName = teamNames[index]; teamsData[index] = teamsData[index+1]; teamNames[index] = teamNames[index+1]; teamsData[index+1] = tempArray; teamNames[index+1] = tempName; swapCounter++; //System.out.println("Swap made at index"+ index+" on pass "+zz); } } //System.out.println("On pass "+zz+" Swaps made :"+ swapCounter); } } public static int storeFileData(Scanner inFile, int MAXTEAMS, int[][] teamsData, String[][] teamNames) { final int TEAMNUM = 0; // column 0 is the team number final int TEAMRP = 1; // column 1 is the team RP final int TEAMQP = 2; // column 2 is the team QP final int TEAMRANK = 3; final int TEAMNAME = 1; final int TEAMSTATUS = 0; boolean invalidData= false; boolean noStatus = false; boolean noName = false; int numberOfTeams = 0; String inputLine = ""; Scanner inLine = new Scanner(inputLine); String inWord = ""; int i; //System.out.println("In storeFileData"); for(i = 0; (i < MAXTEAMS) && (inFile.hasNextLine()); i++) { //System.out.println("In for loop"); inputLine = inFile.nextLine(); // read in one whole line invalidData = false; noStatus = false; noName = false; inLine = new Scanner(inputLine); // set up to read from line if (inLine.hasNextInt()) // is there an int at beginning of line? { //System.out.println("first has next int"); teamsData[i][TEAMNUM] = inLine.nextInt(); if (inLine.hasNext() ) { inWord = inLine.next(); try // next check for int ranking points RP { teamsData[i][TEAMRP] = Integer.parseInt(inWord); } catch (NumberFormatException nfe) { invalidData = true; // No RP value } } else { invalidData = true; // no word } if (inLine.hasNextInt() && !invalidData) // check for QP { teamsData[i][TEAMQP] = inLine.nextInt(); } else // no int for qualifying points found { invalidData = true; // No QP value } if ((!invalidData) && (inLine.hasNext()) && !(inLine.hasNextInt())) // reads team status word { teamNames[i][TEAMSTATUS] = inLine.next().trim(); if (!teamNames[i][TEAMSTATUS].equals("veteran") && !teamNames[i][TEAMSTATUS].equals("rookie") ) { invalidData = true; } } else if ((!invalidData)&& !(inLine.hasNextInt())) { invalidData = true; // no team status value and no rank could be read } else if ((!invalidData) && (inLine.hasNextInt())) { noStatus = true; // no team status value could be read teamNames[i][TEAMSTATUS] = ""; } if ((!invalidData) && !noStatus && !(inLine.hasNextInt())) { inLine.useDelimiter("\\d+"); //System.out.println("in Team name read"); teamNames[i][TEAMNAME] = inLine.next(); //System.out.println("Team name "+teamNames[i][TEAMNAME]); inLine.reset(); } else if ((!invalidData)&& (!inLine.hasNextInt())) { invalidData = true; // no team status value could be read } else if ((!invalidData)&& (inLine.hasNextInt())) { noName = true; // no team name value could be read teamNames[i][TEAMNAME] = ""; } if ((!invalidData) && (inLine.hasNextInt())) { teamsData[i][TEAMRANK] = inLine.nextInt(); } else { invalidData = true; // no team status value could be read } } else // the line didn't start with an int (team number) { invalidData = true; // No team number } // if bad data was found, then we want to write over // whateve might have been read from the file // so we set the counter back by one if (invalidData) { i--; } } numberOfTeams = i; return numberOfTeams; } public static boolean removeTeam(int[][] teamsData, String[][] teamNames, int teamNumber) { final int TEAMNAME = 1; // column 1 is the team name final int TEAMSTATUS = 0; // column 0 is the team status (rookie or veteran) int colData = 3; /* final int TEAMNUM = 0; // column 0 is the team number final int TEAMRP = 1; // column 1 is the team RP final int TEAMQP = 2; // column 2 is the team QP */ boolean success = true; int teamCount = 0; for (int k=0; k < teamsData.length; k++) { if (teamsData[k][TEAMNUM] != 0) teamCount++; } System.out.println(); //System.out.println("Team count is "+teamCount); if (teamNumber > 0) { System.out.println("Removing team "+teamNumber); boolean found = false; // binary search int low = 0; int high = teamCount - 1; int pos = 0; while (low <= high && !found) { pos = (low + high) / 2; // Midpoint of the subsequence //System.out.println("TeamsData["+pos+"][TEAMNUM] is "+teamsData[pos][TEAMNUM]); if (teamsData[pos][TEAMNUM] == teamNumber) { found = true; } else if (teamsData[pos][TEAMNUM] < teamNumber) { low = pos + 1; } // Look in second half else { high = pos - 1; } // Look in first half } if (found) { System.out.println("Found at position " + pos); /*remove element at pos*/ teamsData[pos][TEAMNUM] = 0; teamsData[pos][TEAMRP] = 0; teamsData[pos][TEAMQP] = 0; teamNames[pos][TEAMNAME] = ""; teamNames[pos][TEAMSTATUS] = ""; bubbleSortTeamNumsHi2Lo (teamsData, teamNames, colData, TEAMNUM); bubbleSortTeamNums (teamsData, teamNames, colData, TEAMNUM); } else { System.out.println("Item not found. "); success = false; } } else { if (teamNumber < 0) System.out.println("Invalid team number to remove. "); success = false; } return success; } public static boolean addTeam(int[][] teamsData, String[][] teamNames, int teamNumber) { final int TEAMNAME = 1; // column 1 is the team name final int TEAMSTATUS = 0; // column 0 is the team status (rookie or veteran) int colData = 3; /* final int TEAMNUM = 0; // column 0 is the team number final int TEAMRP = 1; // column 1 is the team RP final int TEAMQP = 2; // column 2 is the team QP */ boolean success = true; int teamCount = 0; for (int k=0; k < teamsData.length; k++) { if (teamsData[k][TEAMNUM] != 0) teamCount++; } System.out.println(); if (teamNumber > 0) { //System.out.println("Team count is "+teamCount); System.out.println("Adding team "+teamNumber); boolean found = false; int low = 0; int high = teamCount - 1; int pos = 0; while (low <= high && !found) { pos = (low + high) / 2; // Midpoint of the subsequence //System.out.println("TeamsData["+pos+"][TEAMNUM] is "+teamsData[pos][TEAMNUM]); if (teamsData[pos][TEAMNUM] == teamNumber) { found = true; } else if (teamsData[pos][TEAMNUM] < teamNumber) { low = pos + 1; } // Look in second half else { high = pos - 1; } // Look in first half } if (found) { System.out.println("Item already in list. Found at position " + pos); } else { System.out.println("Item not found. Inserting before position " + pos); Scanner userInput = new Scanner(System.in); teamsData[teamCount][TEAMNUM] = teamNumber; System.out.print("Enter the current ranking points for team number "+teamNumber+" : "); if (userInput.hasNextInt()) { teamsData[teamCount][TEAMRP] = userInput.nextInt(); } System.out.print("Enter the current qualifying points for team number "+teamNumber+" : "); if (userInput.hasNextInt()) { teamsData[teamCount][TEAMQP] = userInput.nextInt(); } userInput.nextLine(); System.out.print("Is team number "+teamNumber+" a veteran team or a rookie team? Enter veteran or rookie : "); if (userInput.hasNext()) { teamNames[teamCount][TEAMSTATUS] = userInput.next(); } userInput.nextLine(); System.out.print("Enter the name of team number "+teamNumber+" : "); if (userInput.hasNextLine()) { teamNames[teamCount][TEAMNAME] = userInput.nextLine(); } bubbleSortTeamNumsHi2Lo (teamsData, teamNames, colData, TEAMNUM); bubbleSortTeamNums (teamsData, teamNames, colData, TEAMNUM); } } else { if (teamNumber < 0) System.out.println("Invalid team number to add. "); success = false; } return success; } }