Complete the following program (MemoPadReader) to read multiple memos stored in
ID: 3758785 • Letter: C
Question
Complete the following program (MemoPadReader) 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);
// Prompt to display the next memo if there are more memos in the file
if (___________________)
{
System.out.println("Do you want to read the next memo?");
String ans = console.nextLine();
if (ans.equalsIgnoreCase("n"))
{
done = true;
}
}
}
}
}
Explanation / Answer
Modifications
1.create an instance of File class with file name as parameter
2.read topic from first line
3.read date from second line
4.read message from third line
5.call hasNextLine that returns true if there more lines to read
-------------------------------------------------------------------------------------------------------------------------------------
//Modifications are in bold letters
/**The java program that prompts user to enter
* the name of text file "memo.txt" then reads the memo
* from the file and prompt user to continue
* If user enters n , the exit reading from file
* and close the file scanner*/
//MemoPadReader.java
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
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();
//1.create an instance of File class with file name as parameter
File inFile = new File(inputFileName);
//pass the File object inFile as argument to Scanner class constructor
Scanner in = new Scanner(inFile);
boolean done = false;
while (in.hasNextLine() && !done)
{
//2.read topic from first line
String topic = in.nextLine();
//3.read date from second line
String dateStamp = in.nextLine();
//4.read message from third line
String message =in.nextLine();
System.out.println(topic + " " + dateStamp + " " + message);
// Prompt to display the next memo if there are more memos in the file
//Checking if the file scanner in has next line to read
//5.call hasNextLine that returns true if there more lines to read
if (in.hasNextLine())
{
System.out.println("Do you want to read the next memo?");
//prompt for user answer to read next memo
String ans = console.nextLine();
//Checking if user enters n to stop reading from file
if (ans.equalsIgnoreCase("n"))
{
done = true;
}//end of if condition
}//end of if case
}//end of while loop
//close the file scanner
in.close();
}
}
-------------------------------------------------------------------------------------------------------------------
memo.txt input file
Security
11/01/2015 10:00AM
Information systems-security
Programming
12/01/2015 09:00AM
Java programming-OOP
Networking
13/01/2015 09:00AM
Windows-NT
-------------------------------------------------------------------------------------------------------------------
Sample Output:
Input file: memo.txt
Security
11/01/2015 10:00AM
Information systems-security
Do you want to read the next memo?
y
Programming
12/01/2015 09:00AM
Java programming-OOP
Do you want to read the next memo?
y
Networking
13/01/2015 09:00AM
Windows-NT
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.