/************************************ * Data File Organizer * * By: Andrew Coutts * Date: 1/11/2012 * Version: 1.0 * */ //import classes that will be used later. import java.io.*; import java.util.Scanner; public class DataOrganizer { // declare and initialize our scanner. private Scanner input = new Scanner(System.in); private File dataFile = null; // declare our data file. //************************************************************************** // This method will ask the user for the path to a data file. It checks // if the path is valid and the file exists. If something isn't right, // the user is informed that the path isn't valid and the loop will start // over until the path is valid. When the file path is correct, execution // returns to main(). //************************************************************************** private File selectFile() { String filePath = ""; // file path string used in the while loop. int fileIsValid = 0; // our flag for a valid file location. while (fileIsValid != 1) // loop until our flag is set, meaning the file specified is valid. { System.out.println("Please input the location of the file. Please use an absolute path starting from the root directory and pointing to the file."); filePath = input.nextLine(); // take input from the user for the data file location. dataFile = new File(filePath); // declare the user's path to the data file as a new file object. if (!(dataFile.exists())) // check if the file path is valid, if it isn't, give this error. { System.out.println("ERROR: File Not Found. Please specify a valid data file location."); fileIsValid = 0; // probably not needed, but make sure our flag stays un-set for the file location. } else { System.out.println("File appears to be valid - moving on."); fileIsValid = 1; // if we make it here, it means the file exists and the path the user entered is valid. // set our flag variable so that the loop stops and we return back to main(). } } return dataFile; } //************************************************************************** // This method takes an inputed data file and reads each line of it into // an array, then returns the array. //************************************************************************** private String[] linesToArray(File dataFile) throws FileNotFoundException { Scanner fileScanner = new Scanner(dataFile); // file scanner to read the file line by line. String[] fileLinesArray = new String[20]; // String array to hold the lines of the file. // this for loop reads each line of the file into an element of the fileLinesArray array. for (int i=0; i<14; i++) { fileLinesArray[i] = fileScanner.nextLine(); } return fileLinesArray; } // process one element of the fileLineByLine array into a new array with each element as the logon id, etc (each thing separated by a comma). private String[] processLineItems(String[] LineArray) { String[] cleanedArray = new String[14]; // process each "line" into a new set of arrays for each item on the line. for (int i=0; i<14; i++) { String[] line = null; // new string for each line of the file. line = LineArray[i].split(","); // split array from commas. line[2] = line[2].toLowerCase(); // make logon id lowercase if (line.length > 4) System.out.printf("Line #%d from data file is invalid, discarding. Too many line items.\n", (i + 1)); if (line.length < 4) System.out.printf("Line #%d from data file is invalid, discarding. Too few line items.\n", (i + 1)); if (line[0].length() < 0) System.out.printf("Line #%d from data file is invalid, discarding."); if (line.length == 4 && line[0].length() > 0) //only ideal lines will fit these conditions. cleanedArray[i] = line[0] + "," + line[1] + "," + line[2] + "," + line[3]; //create the "cleaned" array and join everything together. //~ System.out.println(cleanedArray[i]); //debug use only. } return cleanedArray; } //sum the random numbers to figure out the total. private long SumRandomNums(String[] LineArray) { long sumRandomNums = 0; //used as our return value, it must be a long. String[] line = null; //initialize our line string array. for (int i=0; i maxRandomNumRow) { maxRandomNumRow = Long.parseLong(line[3]); rowNum = (i + 1); } } } } return rowNum; } private long minRandomNumber(String[] LineArray) { String[] line = null; long minRandomNumRow = 100000; //some big number that we know is bigger than any of the random #s in the data file. long rowNum = 0; for (int j=0; j<2; j++) //do all of this twice so that we are sure we have compared the entire list of values. { for (int i=0; i