All pastes #2103351 Raw Edit

Anonymous

public text v1 · immutable
#2103351 ·published 2012-01-15 23:39 UTC
rendered paste body
/************************************
 * 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<LineArray.length; i++)
		{
			if (LineArray[i] != null) //ignore empty null lines.
			{
				line = LineArray[i].split(","); //split the array into another array.
				sumRandomNums = sumRandomNums + Long.parseLong(line[3]); //update the total for our sum, this loops as many times as elements the LineArray array has.
			}

		}
		
		sumRandomNums = (long)sumRandomNums; //convert our total to a long so it can be returned as long.
		
		
		return sumRandomNums;
	}
	
	
	//this function computes the average for the random numbers in the file.
	private double averageNumbers(String[] LineArray)
	{
		String[] line = null; //initialize our line array string array.
		double randomAverage = 0;
		
		for (int i=0; i<LineArray.length; i++)
		{
			if (LineArray[i] != null) //ignore null elements in the array.
			{
				line = LineArray[i].split(","); //split array into a new array
				randomAverage = randomAverage + Long.parseLong(line[3]);
			}
		}
			
		
		randomAverage = (double)(randomAverage / LineArray.length);
		
		return randomAverage;
	}
	
	
	private long maxRandomNumber(String[] LineArray)
	{
		String[] line = null;
		long maxRandomNumRow = 0;
		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<LineArray.length; i++)
			{
				if (LineArray[i] != null)
				{
					line = LineArray[i].split(",");
					if (Long.parseLong(line[3]) > 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<LineArray.length; i++) //loop as many times as there are elemetns in the LineArray array (cleaned array).
			{
				if (LineArray[i] != null) //ignore any null elements JUST in case.
				{
					line = LineArray[i].split(","); //split the array into another set of arrays referenced by the loop index.
					if (Long.parseLong(line[3]) < minRandomNumRow) //this will sort out the lowest number.
					{
						minRandomNumRow = Long.parseLong(line[3]); //if we find a lower one, update our minRandomNumRow variable with it.
						rowNum = (i + 1); //also update the row number of it.
					}
				}
			}
		} //this all repeats twice to make sure we cover the list top to bottom.
		
		return rowNum;
	}
	

	public static void main(String[] args) throws FileNotFoundException
	{
		DataOrganizer dorg = new DataOrganizer(); // create a new object of the DataOrganizer class so that we can access non-static methods / variables from the class.
		String[] fileLineByLine = new String[20]; // initialize a string array to hold the file contents.
		String[] cleanedArray = new String[20];
		int lineCounter = 0; //used to count lines in the cleaned array.
		
		// call the selectFile() method to get the file location from the user and declare it as a new file for us to work with.
		dorg.dataFile = dorg.selectFile();
		
		// fill each element of our file line array with the contents of each line of the data file.
		fileLineByLine = dorg.linesToArray(dorg.dataFile);		
		cleanedArray = dorg.processLineItems(fileLineByLine);
		
		System.out.println("\nCleaned array:");
		for (int i=0; i<cleanedArray.length; i++) //this for loop will count how many lines are actually in the cleaned array, ignoring those which are null.
		{
			if (!(cleanedArray[i] == null)) //ignore null elements as they don't exist!!
			{
				System.out.println(cleanedArray[i]);
				lineCounter++;
			}
		}
		
		// all of our print statements. some call some of the methods above, others just print variables who are calculated earlier in main().
		System.out.println("\nTotal number of lines in cleaned array: " + lineCounter);
		System.out.println("Sum of randon numbers: " + dorg.SumRandomNums(cleanedArray));
		System.out.println("Average of random numbers: " + dorg.averageNumbers(cleanedArray));
		System.out.println("Row with the max random number: " + dorg.maxRandomNumber(cleanedArray));
		System.out.println("Row with the min random number: " + dorg.minRandomNumber(cleanedArray));
		
	}
}