Write a program to store multiple memos in a file. Allow a user to enter a topic
ID: 3760714 • Letter: W
Question
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:
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Scanner;
public class MemoPadCreator
{
public static void main(String[] args) throws FileNotFoundException
{
Date now = new Date();
Scanner console = new Scanner(System.in);
System.out.print("Output file: ");
String filename = console.nextLine();
PrintWriter out = new PrintWriter(new File(filename));
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
now = new Date();
Timestamp dateStamp = new Timestamp(now.getTime());
out.println(topic + " " + dateStamp + " " + message);
}
}
// Close the output file
out.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.