/* * Read a text file into an array of Teacher objects * Writes to a text file the date time of when the program was run */ package readingfromatextfile; /** * * @author seilertsen */ public class ReadingUI { public static void main(String[]args) { ReadingManager rm = new ReadingManager(); String allTeachers = ""; System.out.println("Teachers Array"); System.out.println("======================================"); allTeachers = rm.displayAll(); System.out.println(allTeachers); System.out.println("hello main method"); } // end main method } // end class ===================================================================================== /* * Using the method "split" to break a line into tokens * The use of split places each token in the line into an indexed String array * Use parse to save each token into its correct datatype. * Create a singly stand-alone object - then copy the single object into an * array of objects. * * This version appends the date time to a login file using PrintWriter. * */ package readingfromatextfile; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.time.LocalDateTime; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author seilertsen */ public class ReadingManager { // global variables private Teachers[] tArr = new Teachers[50]; private String[] tokens = null; private int size = 0; private boolean certified = false; private PrintWriter pw = null; private LocalDateTime dt = null; // Constructor method // We need IOException in case writting to the file is not possible public ReadingManager()throws IOException { try { // Link to the text file Scanner scFile = new Scanner(new File("myTextFile.txt")); System.out.println("File found.\n"); // Create the Login file to write to // Parameter "true" appends to the end of the file pw = new PrintWriter(new FileWriter("LoginTimes.txt",true)); // name, year, certified, donation; while(scFile.hasNextLine()){ // Read in the whole line before breaking it into tokens String line = scFile.nextLine(); // Using the delimiter "#" the line is broken up into tokens // Each token is automatically placed into the next index of // the String array tokens = line.split("#"); String fullName = tokens[0]; String yearSt = tokens[1]; // The Strings need to be parsed to their correct datatype int year = Integer.parseInt(yearSt); // There is no parse method for boolean if(tokens[2].equals("true")) certified = true; else if (tokens[2].equals("false")) certified = false; else System.out.println("Error"); String donationSt = tokens[3]; double donation = Double.parseDouble(donationSt); // Create the single stand alone object Teachers t = new Teachers(fullName, year, certified, donation); // Copy the stand alone object into the next index of the array tArr[size] = t; // increase the counter size++; } // end while // Record the date time of the login event dt = LocalDateTime.now(); System.out.println("date time: " + dt); //Parse date time to Sting and write to login file pw.println("" + "Event@Login" + "@" + dt); pw.close(); } catch (FileNotFoundException ex) { Logger.getLogger(ReadingManager.class.getName()).log(Level.SEVERE, null, ex); System.out.println("File not found"); } } // end constructor public String displayAll(){ // The format of displayAll is determined by the toString method // in the Teacher class String all = ""; for(int x = 0; x < size; x++) { all = all + tArr[x]; } return all; } // end displayAll } // end Teacher Manager class ==================================================================================================== /* * Template class for Teacher objects * The format of the output is controlled by the structore of the toString * method. */ package readingfromatextfile; /** * * @author seilertsen */ public class Teachers { String name = null; int year = 0; boolean certified = false; double donation = 0.0; public Teachers(String n, int y, boolean c, double d){ name = n; year = y; certified = c; donation = d; } @Override public String toString(){ // The default toString is overrideen by this custom version offering // the tokens in a different order to the text file return donation + "\t" + name + "\t" + year + "\t" + certified + "\n"; } // toString } // end class =========================================================================================== TEXT File - INPUT Steve Eilertsen#1956#false#23.4 Andy Alpha#1987#true#45.7 Betta Bravo#1999#false#12.37 Chunky Charlie#2004#true#56.87 David Delta#2008#true#48.17 ============================================================================================ TEXT FILE - OUTPUT Event@Login@2024-09-16T13:02:39.337369300 Event@Login@2024-09-16T13:05:53.232936500 Event@Login@2024-09-16T13:06:05.223731 Event@Login@2024-09-16T13:06:10.632609200 Event@Login@2024-09-16T13:06:16.147052100 Event@Login@2024-09-16T13:06:35.629899200 Event@Login@2024-09-16T13:06:40.715483800 Event@Login@2024-09-16T13:25:25.835277900 ========================================================================================== OUTPUT TO THE MONITOR run: File found. date time: 2024-09-14T08:57:11.172312900 Teachers Array ====================================== 23.4 Steve Eilertsen 1956 false 45.7 Andy Alpha 1987 true 12.37 Betta Bravo 1999 false 56.87 Chunky Charlie 2004 true 48.17 David Delta 2008 true hello main method BUILD SUCCESSFUL (total time: 0 seconds)