/* * 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 eleventhfall16; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class EleventhFall16 { /** * @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]; 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 = readFileIntoArrays(inFile, MAXTEAMS, teamsData, teamNames); } System.out.println("Number of teams is "+numberOfTeams); // Print matching team numbers and names // *** print team RP and QP System.out.println(); System.out.println(); i = 0; for( int ndex = 0; ndex < MAXTEAMS; ndex++ ) // ( variable declaration : arrayName ) { if (teamsData[ndex][TEAMNUM] > 0) // teams[index] is replaced currTeam { printTeamBanner2D(teamsData[ndex],teamNames[ndex]); } i++; } System.out.println(); System.out.println("Before sorting: "); printTeamTable(teamsData, teamNames,"Count"); 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"); // Write a method that reads from a file with the name // "TeamsRPQPupates" and uses its info to update the array // then print the updated array by team number and then by team rank inputData = new File("TeamsRPQPupdates"); fileFound = true; try { inFile = new Scanner(inputData); } catch (FileNotFoundException fnf) { inFile = new Scanner(System.in); System.out.println("Can't find updates file"); fileFound = false; } int tnum = 0; int tRP = 0; int tQP = 0; boolean invalidData = false; while (inFile.hasNextLine()) { if (inFile.hasNextInt()) { tnum = inFile.nextInt(); } else { invalidData = true; } if ((inFile.hasNextInt()) && !invalidData) { tRP = inFile.nextInt(); } else { invalidData = true; } if ((inFile.hasNextInt()) && !invalidData) { tQP = inFile.nextInt(); } else { invalidData = true; } inFile.nextLine(); System.out.println("Read updates for team "+tnum+" w RP "+tRP+" & QP "+tQP); for (i = 0; i < MAXTEAMS; i++) { if (tnum == teamsData[i][TEAMNUM]) { teamsData[i][TEAMRP] = teamsData[i][TEAMRP] + tRP; teamsData[i][TEAMQP] += tQP; } } } bubbleSortTeamNums (teamsData, teamNames, colData, TEAMNUM); System.out.println(); System.out.println("After updating with team ranking: "); printTeamTable(teamsData, teamNames,"Updated Ranks"); bubbleSortTeamRank(teamsData, teamNames, colData, TEAMNUM); System.out.println(); System.out.println("Sorted with updated team ranking: "); printTeamTable(teamsData, teamNames,"Updated Rank Order"); } 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, number "+teams[TEAMNUM] +", with team name "+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 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 readFileIntoArrays(Scanner inFile, int MAXTEAMS, int[][] teamsData, String[][] teamNames) { int i; String inputLine = ""; Scanner inLine = new Scanner(inputLine); String inWord = ""; boolean invalidData = false; 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; // column 1 is the team name final int TEAMSTATUS = 0; // column 0 is the team status (rookie or veteran) 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--; } } return i; } }