/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code9octf18; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code9OctF18 { /** * @param args the command line arguments */ public static void main(String[] args) { 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; // --- 1. How is dataValid being used? double highestPrice =0; // 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) // --- 2. Compare while, do-while, for do { // Read various pieces of data from file including title dataValid = true; try // --- 3. Why is a try-catch being used here? { //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); // --- 4. Method call //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++; } // --- 6. Write methods to find highest and lowest price to be called in // else of this test. What variables are needed? highestPrice = findHighestPrice(priceD, highestPrice); } if (dataValid) { switch(coverD) // --- 3. Review switch: (test), {body}, case { // constant labels, colon, break;, default case 'H': { numH++; break; } case 'P': { numP++; break; } case 'S': { numS++; break; } default: { 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++; } while (inData.hasNext() && dataValid) ; // --- 5. make method to print earliest and latest year messages yearInfo("earliest", earliestYear,eYCounter); yearInfo("latest",latestYear, lYCounter); System.out.println("Highest price was "+highestPrice); 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"); } // --- 5. Write one method to print earliest year and latest year sentences public static void yearInfo(String whichMax, int year,int counter) { System.out.println("\nThe "+whichMax+" year is "+year + " with "+counter+" replacements"); } // Method to remove extra blanks and tabs from beginning and end of a string 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; } public static double findHighestPrice(double price, double highest) { if (price > highest) { highest = price; } return highest; } }