/******************************************************* * ManipulateDataDriver * This program drives the ManipulateData class. It * calls methods from it that will make the program * read data from a file and sort it into different * arrays according to the data type. ******************************************************/ public class ManipulateDataDriver { /** * main * This method calls other methods from the ManipulateData * class and runs the whole program. * * @param args unused */ public static void main(String[] args) { System.out.println("Welcome to the ManipulateData program.\nThis Program will ask " + "you for a file to get input from, and two files to print output to.\nIt will " + "echo the data it reads in from the file to the screen, and add the data to " + "one \n" + "of two arrays depending on it's data type. The data in the arrays will be \n" + "output to the two text files with the label \"Unsorted\" , and then the two " + "arrays \n" + "will be sorted (lexicographically for Strings, numerically for integers) \n" + "and added to the two different text files with the label \"Sorted\", whose " + "names will \nbe specified by you.\n"); ManipulateData myManipulateData; // Instantiate a ManipulateData object, which runs the constructor myManipulateData = new ManipulateData(); System.out.println("The program has ended normally."); } } import java.util.Scanner; import java.io.*; /********************************************************** * ManipulateData.java * This program reads in a filename from the user, and * reads data from a file which it stores in an array * according to what type of data it is. *********************************************************/ public class ManipulateData { Scanner fileScanner, keyboard; PrintWriter intOutfile, strOutfile; private int numInt, numStr; private String input, outStr, outInt; private String[] myStrings; private int[] myInts; /** * ManipulateData * This method is the constructor method for the class. */ public ManipulateData() { keyboard = new Scanner(System.in); myStrings = new String[100]; myInts = new int[50]; // Call the methods to run the program from the constructor setFileScanner(); setOutfiles(); readData(); writeArray(false); sortStringArray(); sortIntArray(); writeArray(true); remindUser(); // Close both output files strOutfile.close(); intOutfile.close(); } /** * setFileScanner * This method initializes the file scanner to get input from the * file specified by the user. It checks to make sure the file * exists and loops until it gets a valid filename. */ public void setFileScanner() { String file; file = null; boolean isValid; // Initialize isValid to false so that the loop will run initially, and initialize // fileScanner to null to avoid the "may not have been initialized" error isValid = false; //fileScanner = null; // Loop until the user enters a valid filename for file input while(!isValid) { System.out.print("Enter the file name with path included of the input file: "); file = keyboard.nextLine(); System.out.println("You entered the file for input: " + file); // Attempt to open the input file using Scanner try { fileScanner = new Scanner(new File(file)); isValid = true; } catch(FileNotFoundException fnfe) { System.out.println("File not found."); isValid = false; } } // Put filename in input input = file; // Tell fileScanner to use other delimiters rather than the default fileScanner.useDelimiter("[-\n.,?:;%$]"); } /** * setOutfiles * This method sets the two output files that the user wants to output the * String and integer data to. */ public void setOutfiles() { String file; System.out.print("\nEnter the file name of the String output file: "); file = keyboard.nextLine(); System.out.println("You entered the file for String output: " + file); // Try to open a file to print output to for the Strings try { strOutfile = new PrintWriter(file); } catch(IOException ioe) { System.out.println("Could not open file for writing."); } // Put filename in outStr outStr = file; System.out.print("\nEnter the file name of the integer output file: "); file = keyboard.nextLine(); System.out.println("You entered the file for integer output: " + file); // Try to open a file to print output to for the integers try { intOutfile = new PrintWriter(file); } catch(IOException ioe) { System.out.println("Could not open file for writing."); } // Put filename in outInt outInt = file; } /** * readData * This method reads in the data from the file and stores it either in * the String array or the integer array. */ public void readData() { String temp; System.out.println("\nData from file: "); // While there is data in the file, loop and store it into the // correct array, while incrementing the number of elements that // are in the array. Echo the data from the file to the screen also. while(fileScanner.hasNext()) { if(fileScanner.hasNextInt()) { myInts[numInt] = fileScanner.nextInt(); System.out.println("Integer: " + myInts[numInt]); numInt++; } else { temp = fileScanner.next(); if(!temp.equals("")) { myStrings[numStr] = temp; System.out.println("String: " + myStrings[numStr]); numStr++; } } } } /** * writeArray * This method writes two arrays to the files specified by the user. * * @param sorted true if array is sorted, false if unsorted */ public void writeArray(boolean sorted) { // Print the correct header whether the array is sorted or unsorted if(sorted) { strOutfile.println("******Sorted******"); intOutfile.println("******Sorted******"); } else { strOutfile.println("******Unsorted******"); intOutfile.println("******Unsorted******"); } // Loop until every String array element has been written to the string // text file. for(int i = 0; i < numStr; i++) { strOutfile.println(myStrings[i]); } // Loop until every integer array element has been written to the integer // text file for(int i = 0; i < numInt; i++) { intOutfile.println(myInts[i]); } } /** * sortStringArray * This method sorts an array of strings lexicographically in ascending * order */ public void sortStringArray() { // Sort the Strings using the selection sort algortithm with compareTo() int startScan, index, minIndex; String minValue; for(startScan = 0; startScan < (numStr - 1); startScan++) { minIndex = startScan; minValue = myStrings[startScan]; for(index = startScan + 1; index < numStr; index++) { if(myStrings[index].compareTo(minValue) < 0) { minValue = myStrings[index]; minIndex = index; } } myStrings[minIndex] = myStrings[startScan]; myStrings[startScan] = minValue; } } public void sortIntArray() { // Sort the integers using the selection sort algorithm int startScan, index, minIndex, minValue; for(startScan = 0; startScan < (numInt - 1); startScan++) { minIndex = startScan; minValue = myInts[startScan]; for(index = startScan + 1; index < numInt; index++) { if(myInts[index] < minValue) { minValue = myInts[index]; minIndex = index; } } myInts[minIndex] = myInts[startScan]; myInts[startScan] = minValue; } // Create a temporary array for use in reversing the sorted array int[] tempArray; tempArray = new int[numInt]; // Reverse the array to put the integers in descending order for(int i = 0; i < numInt; i++) tempArray[i] = myInts[i]; for(int i = 0; i < numInt; i++) { myInts[i] = tempArray[(numInt - 1) - i]; } } /** * remindUser * This method reminds the user where the input and output files are */ public void remindUser() { System.out.println("\nThe input file is located at: " + input); System.out.println("The String output file is located at: " + outStr); System.out.println("The integer output file is lcoated at: " + outInt); } }