/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code25sepf18; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code25SepF18 { /** * @param args the command line arguments */ public static void main(String[] args) { File inputFile3 = new File ("bookYearTitleSeriesSciFiSpaces.txt"); // where is the file created and where can NetBeans see it? Scanner inFile3; Scanner input = new Scanner(System.in); // attaching input file to scanner in a try-catch try { inFile3 = new Scanner(inputFile3); } catch (FileNotFoundException fnfe) { inFile3 = new Scanner(System.in); System.out.println("Input file was not found"); } int year3; String title3; String series3; int earliestYear = 3000, latestYear = 0; int eYCounter = 0, lYCounter = 0; boolean dateNotValid = true; // read data until file is empty //for (int j = 0; j < 40; j++) System.out.printf("\n%9s %-30s%-20s\n","Year", "Book Title","Series Name"); System.out.println("_____________________________________________________________________"); //while (inFile3.hasNextInt()) for (int j = 0; (j < 40) && inFile3.hasNextInt(); j++) { // ***Error check the year*** year3 = inFile3.nextInt(); //while (dateNotValid) { dateNotValid = false; // file input if (year3 > 2018) { System.out.println("The publication year "+year3+" is in the future."); year3 = 9999; //dateNotValid = true; //System.out.print("Please re-enter the publication year: "); //year3 = input.nextInt(); } else if (year3 < 1354) { System.out.println("The publication year "+year3+" is before the advent of the printing press."); year3 = 99; //dateNotValid = true; //System.out.print("Please re-enter the publication year: "); //year3 = input.nextInt(); } } dateNotValid = true; //System.out.printf("%3d%6d\n",j+1, year3); if ((year3 < earliestYear) && (year3 != 99)) { earliestYear = year3; eYCounter++; } if ((year3 > latestYear) && (year3 != 9999 )) { latestYear = year3; lYCounter++; } // ignoring the titles and series inFile3.useDelimiter(":"); title3 = inFile3.next(); inFile3.reset(); title3 = removeBlanks(title3); inFile3.next(); // removes the colon : series3 = inFile3.nextLine(); series3 = removeBlanks(series3); //System.out.println("Program read in year3 "+ // year3+" and title \""+title3+"\" \tand series "+series3); System.out.printf("%3d%6d %-30s%-30s\n",j+1,year3, title3, series3); } System.out.println("\nThe earliest year is "+earliestYear + " with "+eYCounter+" replacements"); System.out.println("\nThe latest year is "+latestYear + " with "+lYCounter+" replacements\n"); //System.out.println("All data has been read"); } 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; } }