/* * 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 twelfthfall16; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; /** * * @author jcmtiernan */ public class TwelfthFall16 { /** * @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("TeamsRPQP11"); 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 = 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[][] 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; inputData = new File("TeamsRPQPupdates"); 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 = ""; 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(); inWord = inLine.next(); try // next check for int ranking points RP { tRP = Integer.parseInt(inWord); } catch (NumberFormatException nfe) { invalidData = true; // No RP value } if (inLine.hasNextInt()) // check for QP { tQP = inLine.nextInt(); } else // no int for qualifying points found { invalidData = true; // No QP value } //System.out.println("Read "+tnum+" "+tRP+" "+tQP); // go through array to find matching team number for (i = 0; (i < teamsData.length); i++) { if ((teamsData[i][TEAMNUM] == tnum) && (invalidData == false)) { teamsData[i][TEAMRP] += tRP; teamsData[i][TEAMQP] += tQP; //System.out.println("Added "+tRP+" RP and "+tQP+" QP for team "+tnum); } } } 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 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); boolean found = false; int searchedNumber = 8406; 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] == searchedNumber) { found = true; } else if (teamsData[pos][TEAMNUM] < searchedNumber) { 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); System.out.println(); System.out.println("After removing team and resorting : "); printTeamTable(teamsData, teamNames,"Order"); System.out.println(); } else { System.out.println("Not found. Insert before position " + pos); } } public static String removeBlanks(String inString) { while (inString.charAt(0) == ' ') { inString = inString.substring(1); //System.out.println("Removing blank in name["+i+"]"); } return inString; } 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 TEAMNAME = 1; final int TEAMSTATUS = 0; boolean invalidData= 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; 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()) // check for QP { teamsData[i][TEAMQP] = inLine.nextInt(); } else // no int for qualifying points found { invalidData = true; // No QP value } if ((!invalidData)&& (inLine.hasNext())) // reads team status word { teamNames[i][TEAMSTATUS] = removeBlanks(inLine.next()); } else { invalidData = true; // no team status value could be read } if (!invalidData) { teamNames[i][TEAMNAME] = removeBlanks(inLine.nextLine()); } } 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; } }