/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code30octf18arrayvsarraylist; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; /** * Based on Code23OctF18ArrayList * @author jcmtiernan */ public class Code30OctF18ArrayVsArrayList { /** * @param args the command line arguments */ public static void main(String[] args) { String dataFile = "BMTtitleMix.txt"; File inputData = new File (dataFile); Scanner inFile; boolean dataValid = true; // define PrintWriter to allow output PrintWriter outFile = new PrintWriter(System.out); try { outFile = new PrintWriter("BTitle.txt"); } catch (FileNotFoundException fnfe) { System.out.println("No output file found. Data printing to screen."); } String[] inputWords = new String[20]; ArrayList titles = new ArrayList(100); ArrayList bookYears = new ArrayList(100); ArrayList movieYears = new ArrayList(100); ArrayList tvYears = new ArrayList(100); try { inFile = new Scanner(inputData); } catch (FileNotFoundException fnfe) { inFile = new Scanner(System.in); System.out.println("\nInput file \""+dataFile+"\" was not found"); } Scanner inData = new Scanner(System.in); String inString = ""; int line = 0; int bmt = 0; int books = 0; int movies = 0; int tvs = 0; String leastWord = "zzzz"; String greatestWord = "AAAA"; System.out.println("Reading in data from input file"); do { inString = inFile.nextLine(); inData = new Scanner(inString); inputWords = inString.split("\t"); // split assigns each word to an array element in inputWords books = 0; movies = 0; tvs = 0; bmt = 0; for(int i = 0; i < (inputWords.length); i++) { if (!inputWords[i].equals("")) // note that inputWords != "" does not work { //System.out.println("Line "+line+": inputWords["+i+"]: "+inputWords[i]); // Finding alphabetical least and greatest if ((inputWords[i].compareTo(leastWord) < 0) && // compareTo gives <0, =0, or >0 value (inputWords[i].compareTo("9999") > 0)) { leastWord = inputWords[i]; //System.out.println("Current leastWord is : "+leastWord); } if (inputWords[i].compareTo(greatestWord) > 0) // compareTo gives <0, =0, or >0 value { greatestWord = inputWords[i]; //System.out.println("Current greatestWord is : "+greatestWord); } // capture the years // doesn't handle error in numeric data if (inputWords[i].equals("Book")) { bmt++; books++; bookYears.add(line,Integer.valueOf(inputWords[i+1])); //System.out.println("In bookYears, line is "+line+" and i is "+i+" bookYear is "+bookYears.get(line)); } if (inputWords[i].equals("Movie")) { bmt++; movies++; //System.out.println("In movieYears, line is "+line+" and i is "+i); movieYears.add(line, Integer.valueOf(inputWords[i+1])); //System.out.println("movieYears.get(line) is "+movieYears.get(line)); } if (inputWords[i].equals("TV")) { bmt++; tvs++; //System.out.println("In tvYears, line is "+line+" and i is "+i); tvYears.add(line,Integer.valueOf(inputWords[i+1])); } if ((bmt >= 1) && (inputWords[i].charAt(0) > '9') && !(inputWords[i].equals("Book")) && !(inputWords[i].equals("Movie")) && !(inputWords[i].equals("TV"))) { titles.add(line, inputWords[i]); //System.out.println("Added title: "+titles.get(line)+" bmt = "+bmt); if ((books == 1) && (movies == 0) && (tvs == 0)) { movieYears.add(line,0); tvYears.add(line,0); } if ((books == 1) && (movies == 1) && (tvs == 0)) { tvYears.add(line,0); } if ((books == 1) && (movies == 0) && (tvs == 1)) { movieYears.add(line,0); } if ((books == 0) && (movies == 1) && (tvs == 1)) { bookYears.add(line,0); } if ((books == 0) && (movies == 1) && (tvs == 0)) { bookYears.add(line,0); tvYears.add(line,0); } if ((books == 0) && (movies == 0) && (tvs == 1)) { bookYears.add(line,0); movieYears.add(line,0); } } } } line++; } while (inFile.hasNext() && dataValid) ; inFile.close(); //System.out.println("-------------------------------------------------------"); dataValid = true; System.out.println("Making a file containing only titles"); printTitleFile(titles); System.out.println("Make an array to hold 20 titles and an ArrayList to hold 20 titles"); ArrayList titlesAL = new ArrayList(20); //ArrayList yearsAL = new ArrayList(20); String[] titlesArray = new String[20]; Scanner inputFile = new Scanner( System.in ); try { inputFile = new Scanner( new File ("BTitles.txt")); } catch (FileNotFoundException fnfe) { System.out.println("\nInput file of titles was not found"); dataValid = false; } String tempTitle = ""; if (dataValid) { System.out.println("Filling array and ArrayList with data"); for (int i=0; (i < 20) && inputFile.hasNextLine(); i++) { tempTitle = inputFile.nextLine(); titlesArray[i] = tempTitle; titlesAL.add(tempTitle); } System.out.println("\nArrayList of titles printed with sout then with method call:"); System.out.println(titlesAL); printList(titlesAL); System.out.println("\narray of titles printed with method call:"); System.out.println(titlesArray); printList(titlesArray); System.out.println("\nComparing actions on arrays and ArrayLists"); // delete an element at array location 10 System.out.println("\nDelete ArrayList element at index 10: "+titlesAL.get(10)); System.out.println("using ArrayList .remove method "); //names[10] = ""; titlesAL.remove(10); System.out.println("*** after .remove"); printList(titlesAL); System.out.println("\nDelete array element at index 10: "+titlesArray[10]); titlesArray[10] = ""; for (int m=10; m < (titlesArray.length-1); m++) { System.out.println("Moving title "+(m+1)+" into array location "+m); titlesArray[m] = titlesArray[m+1]; titlesArray[m+1] = ""; } System.out.println("*** after for loop moving titles"); printList(titlesArray); // add an element at location 12 System.out.println("\nAdd ArrayList element at index 12: "+"Magic Burns"); System.out.println("using ArrayList .add method "); //for (int i = 0; i < 15; i++) titlesAL.add(12, "Magic Burns"); System.out.println("*** after .add"); printList(titlesAL); System.out.println("\nAdd an element at location 12 - move elements to make space"); int target = 12; for (int m=(titlesArray.length-1); m > target; m--) { if (titlesArray[m].isEmpty()) { System.out.println("Moving title "+(m+1)+" into array location "+m); titlesArray[m] = titlesArray[m-1]; titlesArray[m-1] = ""; } } printList(titlesArray); titlesArray[target] = "Magic Burns"; printList(titlesArray); System.out.println("\nSort an ArrayList by using built-in sort for Collections class"); Collections.sort(titlesAL); printList(titlesAL); } } public static void printList(String[] list) { for (int m=0; m < list.length; m++) { System.out.println(m+": "+list[m]); } } public static void printList(ArrayList list) { for (int m=0; m < list.size(); m++) { if (!list.get(m).equals("")) { System.out.println(m+" - "+list.get(m)); } } } public static void printFile(ArrayList titles,ArrayList bookYears, ArrayList movieYears,ArrayList tvYears, int line) { PrintWriter output = new PrintWriter(System.out); try { output = new PrintWriter("BMTout.txt"); } catch (FileNotFoundException fnfe) { System.out.println("Cannot print output to file."); } output.printf("\n%34s%8s%8s%8s\n","","Book","Movie","TV"); output.printf("\n%34s%8s%8s%8s\n","Title","Publish","Premier","Air"); for(int k=0; k < (line-1); k++) { //System.out.println("bookYears.get("+k+") = "+bookYears.get(k)); if ((bookYears.get(k) != 0) || (movieYears.get(k) != 0) || (tvYears.get(k) != 0)) { output.printf("%3d %-34s%8s%8s%8s\n",k,titles.get(k), (bookYears.get(k) == 0? "": Integer.toString(bookYears.get(k))), (movieYears.get(k) == 0? "": String.valueOf(movieYears.get(k))), (tvYears.get(k) == 0? "": (""+tvYears.get(k)))); } } output.close(); } public static void printTitleFile(ArrayList titles) { PrintWriter output = new PrintWriter(System.out); try { output = new PrintWriter("BTitles.txt"); } catch (FileNotFoundException fnfe) { System.out.println("Cannot print title output to file."); } for(int k=0; k < (titles.size()); k++) { //System.out.println(titles.get(k)); output.println(titles.get(k)); } output.close(); } }