Guest User

Untitled

a guest
May 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. /************************************
  2.  * Data File Organizer
  3.  *
  4.  * By: Andrew Coutts
  5.  * Date: 1/11/2012
  6.  * Version: 1.0
  7.  *
  8.  */
  9.  
  10. //import classes that will be used later.
  11. import java.io.*;
  12. import java.util.Scanner;
  13.  
  14.  
  15. public class DataOrganizer
  16. {
  17.     // declare and initialize our scanner.
  18.     private Scanner input = new Scanner(System.in);
  19.     private File dataFile = null; // declare our data file.
  20.    
  21.    
  22.     //**************************************************************************
  23.     // This method will ask the user for the path to a data file. It checks
  24.     // if the path is valid and the file exists. If something isn't right,
  25.     // the user is informed that the path isn't valid and the loop will start
  26.     // over until the path is valid. When the file path is correct, execution
  27.     // returns to main().
  28.     //**************************************************************************
  29.     private File selectFile()
  30.     {
  31.         String filePath = ""; // file path string used in the while loop.
  32.         int fileIsValid = 0; // our flag for a valid file location.
  33.        
  34.         while (fileIsValid != 1) // loop until our flag is set, meaning the file specified is valid.
  35.         {
  36.             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.");
  37.            
  38.             filePath = input.nextLine(); // take input from the user for the data file location.
  39.             dataFile = new File(filePath); // declare the user's path to the data file as a new file object.
  40.            
  41.             if (!(dataFile.exists())) // check if the file path is valid, if it isn't, give this error.
  42.             {
  43.                 System.out.println("ERROR: File Not Found. Please specify a valid data file location.");
  44.                 fileIsValid = 0; // probably not needed, but make sure our flag stays un-set for the file location.
  45.             }
  46.             else
  47.             {
  48.                 System.out.println("File appears to be valid - moving on.");
  49.                 fileIsValid = 1; // if we make it here, it means the file exists and the path the user entered is valid.
  50.                                  // set our flag variable so that the loop stops and we return back to main().
  51.             }
  52.         }
  53.         return dataFile;
  54.     }
  55.    
  56.     //**************************************************************************
  57.     // This method takes an inputed data file and reads each line of it into
  58.     // an array, then returns the array.
  59.     //**************************************************************************
  60.     private String[] linesToArray(File dataFile) throws FileNotFoundException
  61.     {
  62.         Scanner fileScanner = new Scanner(dataFile); // file scanner to read the file line by line.    
  63.         String[] fileLinesArray = new String[20]; // String array to hold the lines of the file.
  64.        
  65.         // this for loop reads each line of the file into an element of the fileLinesArray array.
  66.         for (int i=0; i<14; i++)
  67.         {
  68.             fileLinesArray[i] = fileScanner.nextLine();
  69.         }
  70.        
  71.         return fileLinesArray;
  72.        
  73.     }
  74.    
  75.     // 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).
  76.     private void processLineItems(String[] LineArray)
  77.     {
  78.         for (int i=0; i<LineArray.length; i++)
  79.         {
  80.             String[] line = null;
  81.             line = LineArray[i].split(",");
  82.             //line[2].toLowerCase();
  83.             for (int g=0; g<line.length; g++)
  84.             {
  85.             System.out.println(line[g]);
  86.             }
  87.         }
  88.        
  89.     }
  90.  
  91.     public static void main(String[] args) throws FileNotFoundException
  92.     {
  93.         DataOrganizer dorg = new DataOrganizer(); // create a new object of the DataOrganizer class so that we can access non-static methods / variables from the class.
  94.         String[] fileLineByLine = new String[15]; // initialize a string array to hold the file contents.
  95.        
  96.         // call the selectFile() method to get the file location from the user and declare it as a new file for us to work with.
  97.         dorg.dataFile = dorg.selectFile();
  98.        
  99.         // fill each element of our file line array with the contents of each line of the data file.
  100.         fileLineByLine = dorg.linesToArray(dorg.dataFile);
  101.        
  102.         dorg.processLineItems(fileLineByLine);
  103.        
  104.         for (int i=0; i<14; i++)
  105.         {
  106.         //  System.out.printf("fileLineByLine[%d]: %s\n", i, fileLineByLine[i]);
  107.         }
  108.     }
  109. }
Add Comment
Please, Sign In to add comment