/* * 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 thirteenthfall16; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; /** * * @author jcmtiernan */ public class ThirteenthFall16 { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input = new Scanner(System.in); final int MAXTEAMS = 100; 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; 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; 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++) { 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) { int tnum = 0; int tRP = 0; int tQP = 0; int tRank = 0; inputData = new File("TeamsRPQPupdates13"); try { inFile = new Scanner(inputData); } 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 (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; } } } } System.out.println(); // bubble sort bubbleSortTeamNums (teamsData, teamNames, colData, TEAMNUM); System.out.println(); System.out.println("After updating and 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 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(); 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(); } removeTeam(teamsData, teamNames, searchedNumber); 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(); } addTeam(teamsData, teamNames, searchedNumber); System.out.println(); System.out.println("After adding team and resorting : "); printTeamTable(teamsData, teamNames,"Order"); System.out.println(); */ } 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 */ 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]); } } } public static void printTeamBanner(int teamNum, String teamName, String teamStatus, int RP, int QP) { if ((teamNum > 0) && (teamNum < 20000)) { System.out.println(teamStatus +" team, number "+teamNum +", with team name "+teamName +" has ranking points of "+ RP +" and qualifying points of "+QP); } } 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(); 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 } 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 void 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 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); System.out.println("Removing 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("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. "); } } public static void 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 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); 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); } } }