/* * 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 eighthfall16; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class EighthFall16 { /** * @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("TeamsRPQPEighth"); 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"); /* 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]; String inputLine = new String(); Scanner inLine = new Scanner(inputLine); boolean validData = true; // Read in team names and numbers from a file // Going to not read data if there is no file if (fileFound) { // 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++) { inputLine = inFile.nextLine(); inLine = new Scanner(inputLine); if (inLine.hasNextInt()) { teams[i] = inLine.nextInt(); if (inLine.hasNext()) { try { teamsRP[i] = Integer.parseInt(inLine.next()); } catch (NumberFormatException nfe) { validData = false; } } if (validData) { if (inLine.hasNextInt()) { teamsQP[i] = inLine.nextInt(); } else { validData = false; } if (inLine.hasNext()) { teamNames[i] = removeBlanks(inLine.nextLine()); } } } else { i--; } if (!validData) { i--; validData = true; } } } // 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]); */ } 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); } } }