Question: 2.1) Write a program to store multiple memos in a file. Allow a user to enter a topic and the text of a memo (the
2.1) Write a program to store multiple memos in a file. Allow a user to enter a topic and the text of a memo (the text of the memo is stored as a single line and thus cannot contain a return character). Store the topic, the date stamp of the memo, and the memo message. Creating a java.util.Date object with no arguments will initialize the Date object to the current time and date. A date stamp is obtained by calling the Date.toString() method. Use a text editor to view the contents of the output and to check that the information is stored correctly. Part of the program code has been provided for you: import . . . public class MemoPadCreator { public static void main(String[] args) throws FileNotFoundException { Date now; Scanner console = new Scanner(System.in); System.out.print("Output file: "); String filename = console.nextLine(); PrintWriter out = . . .; boolean done = false; while (!done) { System.out.println("Memo topic (enter -1 to end):"); String topic = console.nextLine(); if (topic.equals("-1")) { done = true; } else { System.out.println("Memo text:"); String message = console.nextLine(); // Create the new date object and obtain a dateStamp out.println(topic + " " + dateStamp + " " + message); } } // Close the output file } } 2.2) Modify your memo writing program to read multiple memos stored in a text file. Memos are stored in three lines. The first line is the memo topic, the second line is the date stamp, and the third line is the memo message. Display the memos one at a time, and allow the user to choose to view the next memo (if there is one). Part of the program code has been provided for you: . . . public class MemoPadReader { public static void main(String[] args) throws IOException { Scanner console = new Scanner(System.in); System.out.print("Input file: "); String inputFileName = console.nextLine(); File inFile = . . .; Scanner in = new Scanner(inFile); boolean done = false; while (in.hasNextLine() && !done) { String topic = . . .; String dateStamp = . . .; String message = . . .; System.out.println(topic + " " + dateStamp + " " + message); if (. . .) // You should only ask to display the next memo if // there are more memos in the file { System.out.println("Do you want to read the next memo (y/n)?"); String ans = console.nextLine(); if (ans.equalsIgnoreCase("n")) { done = true; } } } } } 2.3) Modify your simple memo reader program. Use a JFileChooser dialog box to allow the user to choose the file from which the memos will be read. You can use the showOpenDialog method to enable the user to select a file to open. This method returns either JFileChooser.APPROVE_OPTION, if the user has chosen a file, or JFileChooser.CANCEL_OPTION, if the user canceled the selection. If a file was chosen, then you call the getSelectedFile method to obtain a File object that describes the file. Here is a complete example: JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); Scanner in = new Scanner(selectedFile); . . . }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
