/* * steve eilertsen. UI class * OUTPUT in the written to file looks like this . . . * EILERTSEN,S#11 Mar 2012#Male#14:15:52.042 */ package outputtofile; public class OutToFileUI { public static void main(String[] args) { OutToFileManager3 om = new OutToFileManager3(); om.interagateName(); om.interagateID(); String theOutput = om.outToFile(); System.out.println("The output to file is: " + theOutput); } } ==================================================================== /* * steve eilertsen. Manager class * OUTPUT in the written to file looks like this . . . * EILERTSEN,S#11 Mar 2012#Male#14:15:52.042 */ package outputtofile; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.time.LocalTime; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; public class OutToFileManager3 { // global variables. Few as possible . . . LocalTime theTimeIs = null; String name = null, idNumber = null, formalName = null; String formalDate = null; String finalOutputSt = null; public OutToFileManager3(){ theTimeIs = LocalTime.now(); name = JOptionPane.showInputDialog(null, "What is your full name?"); name = name.toUpperCase(); idNumber = JOptionPane.showInputDialog(null, "Enter your 13 digit ID number without spaces"); // Basic error trapping to avoid spaces in the ID number if(idNumber.contains(" ")) { System.out.println("Error with ID number"); System.exit(0); } } public void interagateName() { int space = name.indexOf(" "); String firstName = name.substring(0, space); String lastName = name.substring(space + 1); String initial = name.substring(0,1); formalName = lastName + ","+ initial; } public void interagateID(){ // get the formal date. Local variable work here. String daySt = idNumber.substring(4,6); String monthSt = idNumber.substring(2,4); String yearSt = idNumber.substring(0,2); System.out.println("The month is " + monthSt); /* This has been commented out if(monthSt.equals("01")) monthSt = "Jan"; if(monthSt.equals("02")) monthSt = "Feb"; if(monthSt.equals("03")) monthSt = "Mar"; if(monthSt.equals("04")) monthSt = "Apr"; if(monthSt.equals("05")) monthSt = "May"; if(monthSt.equals("06")) monthSt = "Jun"; if(monthSt.equals("07")) monthSt = "Jul"; if(monthSt.equals("08")) monthSt = "Aug"; if(monthSt.equals("09")) monthSt = "Sep"; if(monthSt.equals("10")) monthSt = "Oct"; if(monthSt.equals("11")) monthSt = "Nov"; if(monthSt.equals("12")) monthSt = "Dec"; formalDate = daySt + " " + monthSt + " " + yearSt; */ // Same as above // Much better alternative to get the formal date // if you leave out the breaks, every statement will execute switch (monthSt){ case "01": { monthSt = "Jan"; break; } case "02": { monthSt = "Feb"; break; } case "03": { monthSt = "Mar"; break; } case "04": { monthSt = "Apr"; break; } case "05": { monthSt = "May"; break; } case "06": { monthSt = "Jun"; break; } case "07": { monthSt = "Jul"; break; } case "08": { monthSt = "Aug"; break; } case "09": { monthSt = "Sep"; break; } case "10": { monthSt = "Oct"; break; } case "11": { monthSt = "Nov"; break; } case "12": { monthSt = "Dec";; break; } default: monthSt = "Err"; break; } // end case // Assumes that no one was born before the year 2000 formalDate = daySt + " " + monthSt + " " + "20" + yearSt; // get the gender. Local variables work here String genderSt = idNumber.substring(6,10); int genderInt = Integer.parseInt(genderSt); genderSt = null; if (genderInt < 5000) genderSt = "Female"; else if (genderInt > 5000) genderSt = "Male"; else genderSt = "Error"; // build the output String here - it is a global variable finalOutputSt = formalName + "#" + formalDate + "#" + genderSt + "#" + theTimeIs; } public String outToFile() { try { PrintWriter outFyl = null; outFyl = new PrintWriter(new FileWriter("details.txt")); outFyl.println(finalOutputSt); outFyl.close(); System.out.println("Success"); } catch (IOException ex) { Logger.getLogger(OutToFileManager3.class.getName()).log(Level.SEVERE, null, ex); } return finalOutputSt; } } =========================================================================== INPUT steve eilertsen 1203115756845 OUTPUT TO File EILERTSEN,S#11 Mar 2012#Male#16:42:24.850