/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code11apr19; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.NoSuchElementException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code11Apr19 { /** * @param args the command line arguments */ final static int LAST = 0; final static int FIRST = 1; final static int SH = 2; ///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; public static void main(String[] args) { /* Multi-dimensional arrays */ // This version handles errors in the input data file File nameAgeFile = new File("namesDatesErr21Mar.txt"); Scanner nameDataFile = new Scanner(System.in); try { nameDataFile = new Scanner(nameAgeFile); } catch (FileNotFoundException fnfe) { System.out.println("No data file found"); } // ### What happens if array is too small? // ### How can we handle this? final int MAXNUM = 50; // format should be lastName, firstName ; SHName pubYr movieYr //String[] lastNm = new String[MAXNUM]; //String[] firstNm = new String[MAXNUM]; //String[] shNm = new String[MAXNUM]; // use constant /* final int LAST = 0; final int FIRST = 1; final int SH = 2; */ String[][] heroNm = new String [MAXNUM][3]; // [ROW][COLUMN] Col [0] = last, col [1] = first, col [2] = shNm /* int[] pubYr = new int[MAXNUM]; int[] movYr = new int[MAXNUM]; int[] age = new int[MAXNUM]; */ /* final int PUBYR = 0; // column indicates the content final int MOVYR = 1; final int AGE = 2; */ int[][] heroNums = new int [MAXNUM][3]; // Initialize all string arrays to prevent crashing due to null pointer values for (int i=0; i testAgeAL = new ArrayList<>(); if (testAgeAL.isEmpty()) System.out.println("\ntestAgeAL has no values"); for (int p=0; p < testIndex; p++ ) { testAgeAL.add(testAge[p]); } if (testAgeAL.isEmpty()) System.out.println("\ntestAgeAL still has no values"); else System.out.println("\nPrinting testAgeAL "+testAgeAL); testAgeAL.add(10); System.out.println("\nPrinting testAgeAL with 10 "+testAgeAL); System.out.println("Length of testAgeAL is "+testAgeAL.size()); Integer val = 40; testAgeAL.remove(val); System.out.println("\nPrinting testAgeAL with 40 removed "+testAgeAL); System.out.println("Length of testAgeAL is "+testAgeAL.size()); testAgeAL.remove(0); System.out.println("\nPrinting testAgeAL with index 0 removed "+testAgeAL); System.out.println("Length of testAgeAL is "+testAgeAL.size()); int testIndexAL = deleteElementAL(testAgeAL,testIndex,77); System.out.println("\nPrinting testAgeAL with value 77 removed "+testAgeAL); System.out.println("Length of testAgeAL is "+testAgeAL.size()+ " or "+ testIndexAL); testAgeAL.add(39); testAgeAL.add(21); testAgeAL.add(9); testAgeAL.add(-4); System.out.println("\nPrinting testAgeAL with values added "+testAgeAL); countSwaps = bubbleSortAgeSwapAL(testAgeAL); System.out.println("\nPrinting testAgeAL after bubblesort "+testAgeAL); */ } 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 deleteElementAL( ArrayList list, int sz, int element) { Integer val = element; list.remove(val); return list.size(); } */ /* method deleteElement removes the passed in element from list and moves all values in list below element up to fill in the space where element was */ /* public static int deleteElement( int[] list,int sz, int element) { int loc = sz; // assume element is at the end // find the location of element for (int i= 0; ((i < sz) && (loc == sz)); i++) { // look for array value that matches element if (list[i] == element) { loc = i; } } // move values below element to fill in its location if (loc < sz) { for(int r = loc; r < sz; r++ ) { list[r] = list[r+1]; } } if (loc == sz) return sz; // element was not in the array else return sz-1; // return new size of array } public static int insertElement( int[] list,int sz, int element) { int loc = sz; // assume new element goes at the end // find the location to add element for (int i= 0; ((i < sz) && (loc == sz)); i++) { // find an element greater than one to be added if (list[i] > element) { loc = i; } } // move any values that might need to be moved if (loc < sz) { for(int r = sz-1; r >= loc; r-- ) { list[r+1] = list[r]; } } // insert/add the new element list[loc] = element; return sz+1; // return new size of array } */ /* 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 bubbleSortAllbySHNameSwap(String[] lastNm, String[] firstNm, String[] shNm,int[] pubYr, int[] movYr, 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 ( (shNm[comp].compareToIgnoreCase(shNm[comp+1])) > 0 ) { // swap swap(list,comp); swap(lastNm, comp); swap(firstNm, comp); swap(shNm, comp); swap(pubYr, comp); swap(movYr, comp); countswaps++; } } } sz = 0; return countswaps; } */ /* public static int bubbleSortAllbyAgeSwap(String[] lastNm, String[] firstNm, String[] shNm,int[] pubYr, int[] movYr, 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] > list[comp+1]) { // swap swap(list,comp); swap(lastNm, comp); swap(firstNm, comp); swap(shNm, comp); swap(pubYr, comp); swap(movYr, comp); countswaps++; } } } sz = 0; return countswaps; } */ /* public static int bubbleSortAllbyAgeSwap( String[][] heroNm,int[] pubYr, int[] movYr, 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] > list[comp+1]) { // swap swap(list,comp); //swap(lastNm, comp); //swap(firstNm, comp); //swap(shNm, comp); swap(heroNm, comp); swap(pubYr, comp); swap(movYr, 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; } */ /* public static int bubbleSortAgeSwap( 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] > list[comp+1]) { // swap swap(list,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 printTableFLS(String[][] heroNm,int[] pubYr, int[] movYr, int[] age, 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], pubYr[i], movYr[i], age[i]); //System.out.printf("%15s %24\n",firstNm[i],lastNm[i]); if (movYr[i] == 0) // just an example of testing something and tableTF = !tableTF; // setting a boolean } return tableTF; } */ /* public static boolean printTableFLS(String[] lastNm, String[] firstNm, String[] shNm,int[] pubYr, int[] movYr, int[] age, 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", shNm[i], firstNm[i], lastNm[i],pubYr[i], movYr[i], age[i]); //System.out.printf("%15s %24\n",firstNm[i],lastNm[i]); if (movYr[i] == 0) // just an example of testing something and tableTF = !tableTF; // setting a boolean } return tableTF; } */ 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; } }