/* * Example for 2D arrays */ package tenthnov10f162d; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * @author jcmtiernan */ public class TenthNov10F162D { /** * @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("TeamsRPQPNinth"); Scanner inFile; boolean fileFound = true; String inputLine = ""; Scanner inLine = new Scanner(inputLine); String inWord = ""; try { inFile = new Scanner(inputData); } catch (FileNotFoundException fnf) { inFile = new Scanner(System.in); System.out.println("Can't find input file"); fileFound = false; } //int[] teams = new int[MAXTEAMS]; String[] teamNames =new String[MAXTEAMS]; //int[] teamsRP = new int[MAXTEAMS]; //int[] teamsQP = new int[MAXTEAMS]; 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]; for (int i = 0; i < MAXTEAMS; i++) for (int j = 0; j < colData; j++) { teamsData[i][j] = 0; } boolean invalidData = false; int i = 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"); // Assumes all data is correct on each line of file // but what happens if data is not OK? 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 } /* // One way to check for and read an int from a string if (!invalidData) { inWord = inLine.next(); try // next check for int qualifying points QP { teamsQP[i] = Integer.parseInt(inWord); } catch (NumberFormatException nfe) { invalidData = true; } } */ // A second way to check for and read an int from a string 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) { teamNames[i] = 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--; } } } int numberOfTeams = i; // Print matching team numbers and names // *** print team RP and QP for (int index = 0; index < teamsData.length ; index++) { if (teamsData[index][TEAMNUM] > 0) { System.out.print("At index ["+index+"] "); } printTeamBanner(teamsData[index][TEAMNUM],teamNames[index], teamsData[index][TEAMRP],teamsData[index][TEAMQP]); } // *** Find the highest ranked team and print it int highestIndex = findHighestTeam(teamsData); System.out.println("The highest ranked team is team "+teamsData[highestIndex][TEAMNUM]); //for (int index = 0; index < teams.length ; index++) System.out.println(); System.out.println("Teams :"); i = 0; for( int ndex = 0; ndex < MAXTEAMS; ndex++ ) // ( variable declaration : arrayName ) { if (teamsData[ndex][TEAMNUM] > 0) // teams[index] is replaced currTeam { System.out.println("Team number at index "+ndex+" - "+teamsData[ndex][TEAMNUM]); } i++; } // bubble sort int temp = -1; int[] tempArray = new int[colData]; 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]; teamsData[index] = teamsData[index+1]; teamsData[index+1] = tempArray; swapCounter++; //System.out.println("Swap made at index"+ index+" on pass "+zz); } } System.out.println("On pass "+zz+" Swaps made :"+ swapCounter); } 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 { //printTeamBanner(teamsData[ndex][TEAMNUM],teamNames[ndex], // teamsData[ndex][TEAMRP],teamsData[ndex][TEAMQP]); printTeamBanner2D(teamsData[ndex],teamNames[ndex]); } i++; } } 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 if ((teams[TEAMNUM] > 0) && (teams[TEAMNUM] < 20000)) { System.out.println("Team number is "+teams[TEAMNUM] +" and team name is "+teamName +" with ranking points of "+ teams[TEAMRP] +" and qualifying points of "+teams[TEAMQP]); } } public static void printTeamBanner(int teamNum, String teamName, int RP, int QP) { if ((teamNum > 0) && (teamNum < 20000)) { System.out.println("Team number is "+teamNum +" and team name is "+teamName +" with ranking points of "+ RP +" and qualifying points of "+QP); } } }