/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code21mar19extra; import java.io.File; import java.io.FileNotFoundException; import java.util.NoSuchElementException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code21Mar19extra { /** * @param args the command line arguments */ public static void main(String[] args) { /* Array declaration refresh Automatic initializations String init (to remove null ptr err) Count of elements vs. size of array Handling “empty” elements Methods with return types Numbers, chars, Booleans Arraylists! */ // 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"); } 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]; int[] pubYr = new int[MAXNUM]; int[] movYr = new int[MAXNUM]; int[] age = new int[MAXNUM]; // Initialize all string arrays to prevent crashing due to null pointer values for (int i=0; i 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 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; } 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[] 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; } }