/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code20sepf18; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code20SepF18 { /** * @param args the command line arguments */ public static void main(String[] args) { File inputFile = new File ("bookYearSciFi.txt"); // where is the file created and where can NetBeans see it? Scanner inFile; Scanner input = new Scanner(System.in); // attaching input file to scanner in a try-catch try { inFile = new Scanner(inputFile); } catch (FileNotFoundException fnfe) { inFile = new Scanner(System.in); System.out.println("Input file was not found"); } int year; // read data until file is empty //for (int j = 0; j < 40; j++) while (inFile.hasNextInt()) { year = inFile.nextInt(); System.out.println("Program read in year "+year); } System.out.println("All data has been read"); File inputFile2 = new File ("bookYearTitleSciFi.txt"); // where is the file created and where can NetBeans see it? Scanner inFile2; // attaching input file to scanner in a try-catch try { inFile2 = new Scanner(inputFile2); } catch (FileNotFoundException fnfe) { inFile2 = new Scanner(System.in); System.out.println("Input file was not found"); } int year2 = 0; String title2 = ""; // read data until file is empty //for (int j = 0; j < 40; j++) while (inFile2.hasNextInt()) { year2 = inFile2.nextInt(); title2 = inFile2.nextLine(); System.out.println("Program read in year2 "+year2+" and title "+title2); } System.out.println("All data has been read from bookYearTitleSciFi"); File inputFile3 = new File ("bookYearTitleSeriesSciFi.txt"); // where is the file created and where can NetBeans see it? Scanner inFile3; // 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; // 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()) int j = 0; for (j = 0; j < 30; j++); { year3 = inFile3.nextInt(); System.out.printf("%3d%6d\n",j+1, year3); if (year3 < earliestYear) { earliestYear = year3; eYCounter++; } if (year3 > latestYear) { latestYear = year3; lYCounter++; } // ignoring the titles and series inFile3.useDelimiter(":"); title3 = inFile3.next(); inFile3.reset(); title3.trim(); inFile3.next(); series3 = inFile3.nextLine(); //System.out.println("Program read in year3 "+ // year3+" and title \""+title3+"\" \tand series "+series3); //System.out.printf("%6d%30s%20s\n",year3, title3, series3); //j++; } 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("All data has been read"); } }