/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code25apr19; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code25Apr19 { /** * @param args the command line arguments */ final static int LAST = 0; final static int FIRST = 1; final static int SH = 2; final static int GENDER = 3; final static int EYE = 4; final static int SPECIES = 5; final static int HAIR = 6; ///int globalIndex = 0; // 1310 global class variable final static int PUBYR = 0; // column indicates the content final static int MOVYR = 1; final static int AGE = 2; final static int HT = 3; final static int WT = 4; final static int HEIGHT = 5; final static int WEIGHT = 6; public static void main(String[] args) { /* Multi-dimensional arrays */ // This version handles errors in the input data file String inputFileName = "namesDatesErr25Apr.txt"; File nameAgeFile = new File(inputFileName); Scanner nameDataFile = new Scanner(System.in); try { nameDataFile = new Scanner(nameAgeFile); } catch (FileNotFoundException fnfe) { System.out.println("No input data file found"); } // Output file PrintWriter nameDataOut = new PrintWriter(System.out); try { nameDataOut = new PrintWriter("Out25Apr19.txt"); nameDataOut.println("Output data from Code15Apr19.java with data \""+inputFileName+"\"\n"); } catch (FileNotFoundException fnfe) { System.out.println("No output data file available"); } PrintWriter nameDataErr = new PrintWriter(System.out); try { nameDataErr = new PrintWriter("Err25Apr19.txt"); nameDataErr.println("Error data from Code15Apr19.java with data \""+inputFileName+"\"\n"); } catch (FileNotFoundException fnfe) { System.out.println("No error data file available"); } // ### What happens if array is too small? // ### How can we handle this? final int MAXNUM = 50; // format should be lastName, firstName ; SHName pubYr movieYr /* final int LAST = 0; final int FIRST = 1; final int SH = 2; */ String[][] heroNm = new String [MAXNUM][7]; // [ROW][COLUMN] Col [0] = last, col [1] = first, col [2] = shNm /* final int PUBYR = 0; // column indicates the content final int MOVYR = 1; final int AGE = 2; */ int[][] heroNums = new int [MAXNUM][5]; // Initialize all string arrays to prevent crashing due to null pointer values for (int i=0; i WEIGHT) // requires knowing data format and number of elements { heroNums[index][HT] = Integer.valueOf(remaining[HEIGHT]); heroNums[index][WT] = Integer.valueOf(remaining[WEIGHT]); } else { valid = false; System.out.println("::: "+origCount+". Missing height or weight data available."); } } catch(NumberFormatException nfe) { valid = false; System.out.println("::: "+origCount+". Height or weight value not an integer"); } } } if (!valid) index--; // if the data isn't valid, ignore the current row of values and replace origCount++; index++; } // end of while (nameDataFile.hasNextLine()) System.out.println("Read "+origCount+" lines from the file"); for (int k = 0; k < index; k++) { heroNums[k][AGE] = heroNums[k][MOVYR] - heroNums[k][PUBYR]; if (heroNums[k][MOVYR] == 0) { heroNums[k][AGE] = 2019 - heroNums[k][PUBYR]; } } System.out.println("\nFile data with calculated age is:"); //printTableFLS(lastNm, firstNm, shNm, pubYr, movYr, age, index); // method call with mult args //printTableFLS(heroNm, pubYr, movYr, age, index); // method call with mult args printTableFLS(heroNm, heroNums, index); printTableFLStoFile(heroNm, heroNums, index, nameDataOut); printTableFLS2(heroNm, heroNums, index); //PrintWriter outputToScreen = new PrintWriter(System.out); printTableFLS2toFile(heroNm, heroNums, index, nameDataOut ); System.out.println(""); /* Sort all arrays together by age using bubble sort with multiple arguments1 Print table to verify */ //int countSwaps = bubbleSortAllbyNumberSwap(heroNm, AGE, heroNums, index); int countSwaps = bubbleSortAllbyAnySwap(2, heroNm, AGE, heroNums, index); // 2 indicates a numeric sort, AGE indicates sort by age //System.out.println("After bubble sort, element count is "+index +" and the number of swaps was "+countSwaps); System.out.println("\nTable after sorting on age: "); nameDataOut.println("\nTable after sorting on age: "); boolean table = printTableFLS(heroNm, heroNums, index); // method call with mult args //System.out.println("The table did "+ (table ? "" : "not ") +"print correctly."); System.out.println("The table had an "+(table?"even ":"odd ")+"number of superheros with no movies"); System.out.println(""); printTableFLStoFile(heroNm, heroNums, index,nameDataOut); //bubbleSortAllbySHNameSwap(lastNm, firstNm, shNm, pubYr, movYr, age, index); //bubbleSortAllbyAnyNameSwap(shNm, firstNm, lastNm, pubYr, movYr, age, index); //bubbleSortAllbyAnyNameSwap(heroNm, SH, heroNums, index); bubbleSortAllbyAnySwap(1, heroNm, SH, heroNums, index); // 1 indicates a String sort, SH indicates sort by superhero name System.out.println("\nTable after shNm sorting using AnyName sort: "); printTableFLS(heroNm, heroNums, index); // method call with mult args System.out.println(""); //bubbleSortAllbyAnyNameSwap(heroNm, LAST, heroNums, index); bubbleSortAllbyAnySwap(1, heroNm, LAST, heroNums, index); System.out.println("\nTable after LastNm sorting using AnyName sort: "); printTableFLS(heroNm, heroNums, index); // method call with mult args System.out.println(""); nameDataOut.println("\nTable after LastNm sorting using AnyName sort: "); printTableFLStoFile(heroNm, heroNums, index,nameDataOut); //bubbleSortAllbyNumberSwap(heroNm, PUBYR, heroNums, index); bubbleSortAllbyAnySwap(2, heroNm, PUBYR, heroNums, index); //System.out.println("After bubble sort, element count is "+index +" and the number of swaps was "+countSwaps); System.out.println("\nTable after sorting on publication year: "); nameDataOut.println("\nTable after sorting on publication year: "); printTableFLS(heroNm, heroNums, index); // method call with mult args printTableFLStoFile(heroNm, heroNums, index, nameDataOut); printTableFLS2(heroNm, heroNums, index); // method call with mult args printTableFLS2toFile(heroNm, heroNums, index, nameDataOut); /* bubbleSortAllbyAnyNameSwap(lastNm, firstNm, shNm, pubYr, movYr, age, index); System.out.println("\nTable after lastNm sorting using AnyName sort: "); printTableFLS(heroNm, pubYr, movYr, age, index); // method call with mult args System.out.println(""); bubbleSortAllbyAnyNameSwap(firstNm, lastNm, shNm, movYr, pubYr, age, index); System.out.println("\nTable after firstNm sorting using AnyName sort: "); printTableFLS(heroNm, pubYr, movYr, age, index); // method call with mult args System.out.println("\n"); */ nameDataErr.close(); // if file isn't closed, no data is written nameDataOut.close(); //outputToScreen.close(); } public static int bubbleSortAllbyAnySwap(int strNum, String[][] heroNm, int colNum, int[][] list, int sz) { // strNum = 1 means Strings, strNum = 2 means ints int countswaps = 0; int temp; String tempStr; for (int pass = 0; pass < sz; pass++) { for (int comp = 0; comp < sz -1 ; comp++) { if (((strNum == 2) && (list[comp][colNum] > list[comp+1][colNum])) || ((strNum == 1) && ( (heroNm[comp][colNum].compareToIgnoreCase(heroNm[comp+1][colNum])) > 0 ))) { // swap swap(list,comp); swap(heroNm, comp); countswaps++; } } } sz = 0; return countswaps; } /* public static int bubbleSortAgeSwapAL( ArrayList list) { int countswaps = 0; int temp; String tempStr; for (int pass = 0; pass < list.size(); pass++) // sz would list.length() { for (int comp = 0; comp < list.size() -1 ; comp++) { if (list.get(comp) > list.get(comp+1)) { // swap swap(list,comp); countswaps++; } } } return countswaps; } private static void swap(ArrayList list, int comp) { Integer temp; //System.out.println("In int swap, trade "+list[comp]+" and "+list[comp+1]); //System.out.println("In Integer swap, trade "+list.get(comp)+" and "+list.get(comp+1)); //temp = list[comp]; temp = list.get(comp); //list[comp] = list[comp+1]; list.set(comp,list.get(comp+1)); //list[comp+1] = temp; list.set(comp+1,temp ); } */ /* public static int bubbleSortAllbyAnyNameSwap(String[] Nm1, String[] Nm2, String[] Nm3,int[] Yr1, int[] Yr2, int[] list, int sz) { int countswaps = 0; int temp; String tempStr; for (int pass = 0; pass < sz; pass++) { for (int comp = 0; comp < sz -1 ; comp++) { //do you need to check for blank strings? if ( (Nm1[comp].compareToIgnoreCase(Nm1[comp+1])) > 0 ) { // swap //System.out.print("Nm1["+comp+"] = "+Nm1[comp]); //System.out.println(" compared to Nm1["+(comp+1)+"] = "+Nm1[comp+1]); swap(list,comp); swap(Nm1, comp); swap(Nm2, comp); swap(Nm3, comp); swap(Yr1, comp); swap(Yr2, comp); countswaps++; } } } sz = 0; return countswaps; } public static int bubbleSortAllbyAnyNameSwap(String[][] heroNm, int colNum, int[][] list, int sz) { int countswaps = 0; int temp; String tempStr; for (int pass = 0; pass < sz; pass++) { for (int comp = 0; comp < sz -1 ; comp++) { //do you need to check for blank strings? if ( (heroNm[comp][colNum].compareToIgnoreCase(heroNm[comp+1][colNum])) > 0 ) { // swap //System.out.print("Nm1["+comp+"] = "+Nm1[comp]); //System.out.println(" compared to Nm1["+(comp+1)+"] = "+Nm1[comp+1]); swap(list,comp); swap(heroNm, comp); countswaps++; } } } sz = 0; return countswaps; } */ /* public static int bubbleSortAllbyAgeSwap( String[][] heroNm,int[][] list, int sz) { int countswaps = 0; int temp; String tempStr; for (int pass = 0; pass < sz; pass++) // sz would list.length() { for (int comp = 0; comp < sz -1 ; comp++) { if (list[comp][AGE] > list[comp+1][AGE]) { // swap swap(list,comp); swap(heroNm, comp); countswaps++; } } } sz = 0; return countswaps; } */ /* public static int bubbleSortAllbyNumberSwap( String[][] heroNm,int colNum, int[][] list, int sz) { int countswaps = 0; int temp; String tempStr; for (int pass = 0; pass < sz; pass++) // sz would list.length() { for (int comp = 0; comp < sz -1 ; comp++) { if (list[comp][colNum] > list[comp+1][colNum]) { // swap swap(list,comp); swap(heroNm, comp); countswaps++; } } } sz = 0; return countswaps; } */ private static void swap(int[][] list, int comp) { int[] temp; //System.out.println("In int swap, trade "+list[comp]+" and "+list[comp+1]); temp = list[comp]; list[comp] = list[comp+1]; list[comp+1] = temp; } private static void swap(String[][] list, int comp) { String[] temp; //System.out.println("In int swap, trade "+list[comp]+" and "+list[comp+1]); temp = list[comp]; list[comp] = list[comp+1]; list[comp+1] = temp; } /* private static void swap(int[] list, int comp) { int temp; //System.out.println("In int swap, trade "+list[comp]+" and "+list[comp+1]); temp = list[comp]; list[comp] = list[comp+1]; list[comp+1] = temp; } private static void swap(String[] list, int comp) { String temp; //System.out.println("In String swap, trade "+list[comp]+" and "+list[comp+1]); temp = list[comp]; list[comp] = list[comp+1]; list[comp+1] = temp; } private static void swap(double[] list, int comp) { double temp; temp = list[comp]; list[comp] = list[comp+1]; list[comp+1] = temp; return; } */ public static boolean printTableFLS(String[][] heroNm,int[][] heroNums, int sz) { boolean tableTF = true; // printing for verification System.out.printf("\n%20s %12s %17s %5s %5s %5s\n","SHName","First","Last","Comic","Movie","Age"); for(int i=0; i < 80; i++) System.out.print("-"); System.out.println(""); for(int i=0; i < sz; i++) { System.out.printf("%20s %12s %17s %5d %5d %5d\n", heroNm[i][SH], heroNm[i][FIRST], heroNm[i][LAST], heroNums[i][PUBYR], heroNums[i][MOVYR], heroNums[i][AGE]); //System.out.printf("%15s %24\n",firstNm[i],lastNm[i]); if (heroNums[i][MOVYR] == 0) // just an example of testing something and tableTF = !tableTF; // setting a boolean } return tableTF; } public static boolean printTableFLStoFile(String[][] heroNm,int[][] heroNums, int sz, PrintWriter outfile) { boolean tableTF = true; // printing for verification outfile.printf("\n%20s %12s %17s %5s %5s %5s\n","SHName","First","Last","Comic","Movie","Age"); for(int i=0; i < 80; i++) outfile.print("-"); outfile.println(""); for(int i=0; i < sz; i++) { outfile.printf("%20s %12s %17s %5d %5d %5d\n", heroNm[i][SH], heroNm[i][FIRST], heroNm[i][LAST], heroNums[i][PUBYR], heroNums[i][MOVYR], heroNums[i][AGE]); //System.out.printf("%15s %24\n",firstNm[i],lastNm[i]); if (heroNums[i][MOVYR] == 0) // just an example of testing something and tableTF = !tableTF; // setting a boolean } return tableTF; } public static void printTableFLS2(String[][] heroNm,int[][] heroNums, int sz) { // printing for verification System.out.printf("\n%20s %12s %17s %5s %5s %5s %7s %7s %17s %12s %4s %4s\n", "SHName","First","Last","Comic","Movie","Age","Sex","Eye","Spec","Hair","Ht","Wt"); for(int i=0; i < 131; i++) System.out.print("-"); System.out.println(""); for(int i=0; i < sz; i++) { System.out.printf("%20s %12s %17s %5d %5d %5d %7s %7s %17s %12s %4d %4d\n", heroNm[i][SH], heroNm[i][FIRST], heroNm[i][LAST], heroNums[i][PUBYR], heroNums[i][MOVYR], heroNums[i][AGE], heroNm[i][GENDER], heroNm[i][EYE], heroNm[i][SPECIES],heroNm[i][HAIR], heroNums[i][HT], heroNums[i][WT] ); } } public static void printTableFLS2toFile(String[][] heroNm,int[][] heroNums, int sz, PrintWriter outfile) { // printing for verification outfile.printf("\n%20s %12s %17s %5s %5s %5s %7s %7s %17s %12s %4s %4s\n", "SHName","First","Last","Comic","Movie","Age","Sex","Eye","Spec","Hair","Ht","Wt"); for(int i=0; i < 131; i++) outfile.print("-"); outfile.println(""); for(int i=0; i < sz; i++) { outfile.printf("%20s %12s %17s %5d %5d %5d %7s %7s %17s %12s %4d %4d\n", heroNm[i][SH], heroNm[i][FIRST], heroNm[i][LAST], heroNums[i][PUBYR], heroNums[i][MOVYR], heroNums[i][AGE], heroNm[i][GENDER], heroNm[i][EYE], heroNm[i][SPECIES],heroNm[i][HAIR], heroNums[i][HT], heroNums[i][WT] ); } } public static String removeBlanks(String inStr) { //System.out.println("In rB, string is *"+inStr+"*"); //String nStr = inStr.trim(); String nStr = inStr; //System.out.println("In rB, string after trim is *"+nStr+"*"); int cnt = 0; if (nStr.length() > 0) { //System.out.println("in rB, in if, before 1st while, nStr.length() = "+nStr.length()+" & cnt = "+cnt); while ((nStr.length() > 0) && (nStr.charAt(cnt) == ' ') ) // check lenght BEFORE checking charAt when string is too short { //System.out.println("in rB, in if, in 1st while, nStr.length() = "+nStr.length()+" & cnt = "+cnt); nStr = nStr.substring(1); //System.out.println("in rB, in if, in 1st while after subs, nStr.length() = "+nStr.length()+" & cnt = "+cnt); } //System.out.println("In rB, string after remove front blanks is *"+nStr+"*"); } if (nStr.length() > 0) { //System.out.println("in rB, in 2nd if, before while, nStr.length() = "+nStr.length()); while ((nStr.length()>0) && (nStr.charAt(nStr.length()-1) == ' ') ) { nStr = nStr.substring(0, nStr.length()-1); } } //System.out.println("In rB, string after remove back blanks is *"+nStr+"*"); return nStr; } }