/* * 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 ninthfall16; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class NinthFall16 { /** * @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"); /* System.out.println(); System.out.println("Please enter an FTC team number followed by the RP, the QP," + " and then the team name. Separate these with a space. \n" + " Enter -1 for the team number if you are finished entering teams."); */ fileFound = false; } int[] teams = new int[MAXTEAMS]; String[] teamNames =new String[MAXTEAMS]; int[] teamsRP = new int[MAXTEAMS]; int[] teamsQP = new int[MAXTEAMS]; boolean invalidData = false; // 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(int 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"); teams[i] = inLine.nextInt(); inWord = inLine.next(); try // next check for int ranking points RP { teamsRP[i] = 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 { teamsQP[i] = 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--; } /* teams[i] = inFile.nextInt(); teamsRP[i] = inFile.nextInt(); teamsQP[i] = inFile.nextInt(); teamNames[i] = removeBlanks(inFile.nextLine()); */ } } // Print matching team numbers and names // *** print team RP and QP for (int index = 0; index < teams.length ; index++) { if (teams[index] > 0) { System.out.print("At index ["+index+"] "); } printTeamBanner(teams[index],teamNames[index],teamsRP[index],teamsQP[index]); } // *** Find the highest ranked team and print it int highestIndex = findHighestTeam(teamsRP, teamsQP); System.out.println("The highest ranked team is team "+teams[highestIndex]); //for (int index = 0; index < teams.length ; index++) System.out.println("Teams :"); for( int currTeam : teams) // ( variable declaration : arrayName ) { if (currTeam > 0) // teams[index] is replaced currTeam { System.out.println("Team number "+currTeam); } } // bubble sort int temp = -1; int swapCounter = 1; int comparisons = (teams.length); //passes for (int zz = 0; (zz < teams.length) && (swapCounter > 0) ; zz++) { //System.out.println("Swaps made :"+ swapCounter); swapCounter = 0; comparisons--; //comparisons for (int index = 0; index < comparisons ; index++) { if (teams[index] > teams[index+1]) //how many times do we do this test? 100x 100 = 10,000 { temp = teams[index]; teams[index] = teams [index+1]; teams[index+1] = temp; swapCounter++; } } } System.out.println("Swaps made :"+swapCounter+" Teams :"); for( int currTeam : teams) // ( variable declaration : arrayName ) { if (currTeam > 0) // teams[index] is replaced currTeam { System.out.println("Team number "+currTeam); } } } 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[] RP, int[] QP) { /* 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; /* Loop through the RP array */ for (int i = 0; i < RP.length; i++) { // if (RP[i] > highRP) //(RP[i] > RP[highIndex]) if ((RP[i] > highRP) || ((RP[i] == highRP) && (QP[i] > highQP))) { highRP = RP[i]; highQP = QP[i]; highIndex = i; } /* else if ((RP[i] == highRP) && (QP[i] > highQP)) { highRP = RP[i]; highQP = QP[i]; highIndex = i; } */ } return highIndex; } 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); } } }