/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code7mar19; import java.io.File; import java.io.FileNotFoundException; import java.util.NoSuchElementException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code7Mar19 { /** * @param args the command line arguments */ public static void main(String[] args) { File nameAgeFile = new File("namesDates7Mar.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]); } catch (NoSuchElementException nsee) { System.out.println("::: 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("::: Year data is invalid"); valid = false; } catch (NoSuchElementException nsee) { System.out.println("::: No data available on current line"); valid = false; } } index++; } // end of while (nameDataFile.hasNextLine()) /* System.out.println("PubYr MovYr"); for (int k = 0; k < index; k++) { System.out.println(pubYr[k]+"\t"+movYr[k]); } */ /* After all data read in, create array age Duplicate array age to secondAge Sort array secondAge alone using bubble sort method Sort all arrays together by age using bubble sort with multiple arguments1 */ for (int k = 0; k < index; k++) { age[k] = movYr[k] - pubYr[k]; } int[] secondAge = new int[index]; for (int k = 0; k < index; k++) // copying an array { secondAge[k] = age[k]; } // Assume index is size of array // Bubble sort secondAge System.out.println("\nSecondAge array before sorting:"); for (int n = 0; n < index;n++) System.out.println(secondAge[n]); System.out.println(""); int tempNum = 0; int numCompares = index; int countCompares = 1; int countSwaps = 1; for(int pass=0; (pass < index) && (countSwaps > 0); pass++) // the number of passes { numCompares--; countSwaps = 0; for( int comp =0; comp < numCompares; comp++) // the comparisons { System.out.println(countCompares+ ". compare secondAge["+comp+"] > secondAge["+(comp+1)+"]"); countCompares++; if (secondAge[comp] > secondAge[comp+1]) { tempNum = secondAge[comp]; secondAge[comp] = secondAge[comp+1]; secondAge[comp+1] = tempNum; countSwaps++; } } } System.out.println("\n\nSecondAge array after sorting:"); for (int n = 0; n < index;n++) System.out.println(secondAge[n]); // printing for verification printTableFLS(lastNm, firstNm, shNm, pubYr, movYr, age, index); /* 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 < index; 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]); } */ /* Sort all arrays together by age using bubble sort with multiple arguments1 */ } 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; } }