Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************
- * 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 void processLineItems(String[] LineArray)
- {
- for (int i=0; i<LineArray.length; i++)
- {
- String[] line = null;
- line = LineArray[i].split(",");
- //line[2].toLowerCase();
- for (int g=0; g<line.length; g++)
- {
- System.out.println(line[g]);
- }
- }
- }
- 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[15]; // initialize a string array to hold the file contents.
- // 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);
- dorg.processLineItems(fileLineByLine);
- for (int i=0; i<14; i++)
- {
- // System.out.printf("fileLineByLine[%d]: %s\n", i, fileLineByLine[i]);
- }
- }
- }
Add Comment
Please, Sign In to add comment