/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code19mar19; import java.io.File; import java.io.FileNotFoundException; import java.util.NoSuchElementException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code19Mar19 { /** * @param args the command line arguments */ public static void main(String[] args) { File nameAgeFile = new File("namesDates19Mar.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]; String oneLine = ""; Scanner inLine; int index = 0; boolean valid = true; String number = ""; while (nameDataFile.hasNextLine()) { valid = true; inLine = new Scanner(nameDataFile.nextLine()); /* Read all inputs as Strings Use delimiters as needed to seperate values Convert Strings to numbers Store each data element into appropriate array */ try { inLine.useDelimiter(", "); lastNm[index] = inLine.next(); lastNm[index] = removeBlanks(lastNm[index]); inLine.reset(); inLine.next(); // read comma inLine.useDelimiter(" : "); firstNm[index] = inLine.next(); //System.out.println("firstNm["+index+"] is "+firstNm[index]); firstNm[index] = removeBlanks(firstNm[index]); inLine.reset(); inLine.next(); // read colon inLine.useDelimiter("[+0-9]"); shNm[index] = inLine.next(); shNm[index] = removeBlanks(shNm[index]); // use the line below (with the "*") when you are NOT using // removeBlanks in order to see all the blanks around the // name and how they change the name sort //shNm[index] = "*"+shNm[index]+"*"; } catch (NoSuchElementException nsee) { System.out.println("::: "+index+". No data available on current line"); valid = false; } inLine.reset(); // reading numbers as strings and parsing if (valid && inLine.hasNext()) { try { number = inLine.next(); //System.out.println("number (pubYr) = "+number); pubYr[index] = Integer.valueOf(number); //Integer assigned to int //System.out.println("pubYr["+index+"] is "+pubYr[index]); number = inLine.next(); //System.out.println("number (movYr) = "+number); movYr[index] = Integer.parseInt(number); // int //System.out.println("movYr["+index+"] is "+movYr[index]); } catch (NumberFormatException nfe) { System.out.println("::: "+index+". Year data is invalid"); valid = false; } catch (NoSuchElementException nsee) { System.out.println("::: "+index+". Insufficient data available on current line"); valid = false; } } index++; } // end of while (nameDataFile.hasNextLine()) for (int k = 0; k < index; k++) { age[k] = movYr[k] - pubYr[k]; } System.out.println("File data with calculated age is:"); printTableFLS(lastNm, firstNm, shNm, pubYr, movYr, age, index); // method call with mult args System.out.println(""); /* Duplicate array age to secondAge */ /* int[] secondAge = new int[index]; for (int k = 0; k < index; k++) // copying an array { secondAge[k] = age[k]; } System.out.println("\nSecondAge array before sorting:"); for (int n = 0; n < index;n++) System.out.println(secondAge[n]); System.out.println(""); */ /* Sort array secondAge alone using bubble sort method Print to verify */ /* bubbleSortAge(secondAge); System.out.println("\nSecondAge array after sorting:"); for (int n = 0; n < index;n++) System.out.println(secondAge[n]); System.out.println(""); */ /* Sort all arrays together by age using bubble sort with multiple arguments1 Print table to verify */ int countSwaps = bubbleSortAllbyAgeSwap(lastNm, firstNm, shNm, pubYr, movYr, age, index); System.out.println("After bubble sort, index is "+index +" and the number of swaps was "+countSwaps); System.out.println("Table after sorting: "); printTableFLS(lastNm, firstNm, shNm, pubYr, movYr, age, index); // method call with mult args System.out.println(""); bubbleSortAllbySHNameSwap(lastNm, firstNm, shNm, pubYr, movYr, age, index); System.out.println("Table after shNm sorting: "); printTableFLS(lastNm, firstNm, shNm, pubYr, movYr, age, index); // method call with mult args System.out.println(""); } public static void bubbleSortAllbyAge(String[] lastNm, String[] firstNm, String[] shNm,int[] pubYr, int[] movYr, int[] list, int sz) { int temp; String tempStr; for (int pass = 0; pass < sz; pass++) { for (int comp = 0; comp < sz -1 ; comp++) { if (list[comp] > list[comp+1]) { // swap temp = list[comp]; list[comp] = list[comp+1]; list[comp+1] = temp; tempStr = lastNm[comp]; lastNm[comp] = lastNm[comp+1]; lastNm[comp+1] = tempStr; tempStr = firstNm[comp]; firstNm[comp] = firstNm[comp+1]; firstNm[comp+1] = tempStr; tempStr = shNm[comp]; shNm[comp] = shNm[comp+1]; shNm[comp+1] = tempStr; temp = pubYr[comp]; pubYr[comp] = pubYr[comp+1]; pubYr[comp+1] = temp; temp = movYr[comp]; movYr[comp] = movYr[comp+1]; movYr[comp+1] = temp; } } } return; } 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++) { 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++) { 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; temp = list[comp]; list[comp] = list[comp+1]; list[comp+1] = temp; } private static void swap(String[] list, int comp) { String temp; 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 void bubbleSortAge(int[] list) { int temp; for (int pass = 0; pass < list.length; pass++) { for (int comp = 0; comp < list.length -1 ; comp++) { if (list[comp] > list[comp+1]) { // swap temp = list[comp]; list[comp] = list[comp+1]; list[comp+1] = temp; } } } return; } public static void printTableFLS(String[] lastNm, String[] firstNm, String[] shNm,int[] pubYr, int[] movYr, int[] age, int sz) { // printing for verification System.out.printf("\n%15s %10s %10s %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("%15s %10s %10s %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]); } } 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; while (nStr.charAt(cnt) == ' ') { nStr = nStr.substring(1); } //System.out.println("In rB, string after remove front blanks is *"+nStr+"*"); while (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; } }