/* * Read a text file into an array of Teacher objects */ 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 * */ package readingfromatextfile; import java.io.File; import java.io.FileNotFoundException; 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; // Constructor method public ReadingManager(){ try { // Link to the text file Scanner scFile = new Scanner(new File("myTextFile.txt")); System.out.println("File found.\n"); // 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 certified = false; 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 along object into the next index of the array tArr[size] = t; // increase the counter size++; } // end while } 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 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(){ return donation + "\t" + name + "\t" + year + "\t" + certified + "\n"; } // toString } // end class ============================================================================================== TEXT File Steve Eilertsen#1956#false#23.4 Andy Alpha#1987#true#45.7 Betta Bravo#1999#false#12.45 Chunky Charlie#2004#true#56.87 =============================================================================================== OUTPUT run: File found. Teachers Array ====================================== 23.4 Steve Eilertsen 1956 false 45.7 Andy Alpha 1987 true 12.45 Betta Bravo 1999 false 56.87 Chunky Charlie 2004 true hello main method BUILD SUCCESSFUL (total time: 0 seconds) ================================================================================================