/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code27sepf18; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code27SepF18 { /** * @param args the command line arguments */ public static void main(String[] args) { /* // where is the file created and where can NetBeans see it? */ String dataFile = "bookSciFiData3.txt"; File inputData = new File (dataFile); // where is the file created and where can NetBeans see it? Scanner inData; int yearD = 0; double priceD = 0; String titleD = "", seriesD =""; char coverD = ' '; String tempD; int earliestPubYr = 1439; int latestPubYr = 2018; int earliestYear = 3000; int latestYear = 0; int eYCounter = 0; int lYCounter = 0; int counter = 1; int numH = 0,numP = 0, numS = 0; int errCount = 0; double avgPriceD = 0; boolean dataValid = true; // attaching input file to scanner in a try-catch try { inData = new Scanner(inputData); } catch (FileNotFoundException fnfe) { inData = new Scanner(System.in); System.out.println("\nInput file \""+dataFile+"\" was not found"); } System.out.printf("\n%8s %-39s%7s %5s %-20s\n","Year", "Book Title","Price","Cover","Series Name"); System.out.println("____________________________________________________________________________________"); while (inData.hasNext() && dataValid) { // Read various pieces of data from file including title dataValid = true; try { //System.out.println("In try yearD"); yearD = inData.nextInt(); } catch (InputMismatchException ime) { dataValid = false; String yearError = inData.next(); System.out.println("****In line "+counter+" - Invalid year data: "+yearError); errCount++; } //System.out.println("in while, counter is "+counter+" and yearD is "+yearD); // error check input year if ((dataValid) && ((yearD > latestPubYr) || (yearD < earliestPubYr)) ) { dataValid = false; System.out.println("****In line "+counter+" - Invalid year: "+yearD); errCount++; } if (dataValid) { //System.out.println("in 1st dataValid before earliestYear"); if (yearD < earliestYear) { earliestYear = yearD; eYCounter++; } if (yearD > latestYear) { latestYear = yearD; lYCounter++; } tempD = inData.next(); coverD = tempD.charAt(0); //System.out.println("coverD is "+coverD); inData.useDelimiter("-*[0-9]"); titleD = inData.next(); inData.reset(); titleD = removeBlanks(titleD); //System.out.println("Title is *"+titleD+"*"); priceD = inData.nextDouble(); //System.out.println("in 1st dataValid before priceD < 0 and price is "+priceD); if (priceD < 0) { dataValid = false; System.out.print("****In line "+counter+" - Invalid price: "); System.out.printf("%.2f\n",priceD); errCount++; } } if (dataValid) { /* if (coverD == 'H') { numH++; } else if (coverD == 'P') { numP++; } else if (coverD == 'S') { numS++; } else { dataValid = false; System.out.println("****In line "+counter+" - Invalid cover type: "+coverD); } */ switch(coverD) // This is an UGLY switch! Pick just one style { case 'H': { numH++; } // Statement after case, in parens, break following break; case 'P': // Statement below case, in parens, break inside parens { numP++; break; } case 'S': numS++; break; // Statement after case, break following, no parens default: // Dr. T prefers statement below case, in parens, break inside parens { dataValid = false; System.out.println("****In line "+counter+" - Invalid cover type: "+coverD); errCount++; break; } } //System.out.println("in 2nd dataValid before avgPriceD"); avgPriceD = avgPriceD + priceD; seriesD = inData.nextLine(); seriesD = removeBlanks(seriesD); //System.out.println("Series is *"+seriesD+"*"); } //System.out.println("after counter increment: "+counter); if (!dataValid) { //System.out.println("in !dataValid"); inData.nextLine(); dataValid = true; } else { System.out.printf("%2d%6d %-37s %8.2f %3c %-30s\n", counter, yearD, titleD, priceD, coverD, seriesD); } counter++; } System.out.println("\nThe earliest year is "+earliestYear + " with "+eYCounter+" replacements"); System.out.println("\nThe latest year is "+latestYear + " with "+lYCounter+" replacements"); System.out.println("\nThere were "+numH+" hardbacks, "+numP +" paperbacks, and "+numS+" softcover books on the list"); System.out.printf("\nThe average price of a book on this list is $%.2f\n",(avgPriceD/counter)); System.out.println("\n**** "+errCount+" lines of input had errors"); //System.out.println("All data has been read"); System.out.println("\nData is complete with "+counter+" books.\n"); } public static String removeBlanks(String inStr) { int inStrLen = inStr.length(); //System.out.println("initial inStr is *"+inStr+"*"); int i = 0; while ((inStr.charAt(i) == ' ') || (inStr.charAt(i) == '\t')) { inStr = inStr.substring(1); // remove one blank at front i=0; // make counter start at 0 again inStrLen = inStr.length(); // get length of new string } //System.out.println("middle inStr is *"+inStr+"*"); inStrLen = inStr.length(); i = (inStrLen - 1); while ((inStr.charAt(i) == ' ') || (inStr.charAt(i) == '\t')) { inStr = inStr.substring(0,i); // remove blanks at end i--; } //System.out.println("final inStr is *"+inStr+"*"); return inStr; } }