The Problem Statement In this lab section, you are going to learn how to read da
ID: 3856702 • Letter: T
Question
The Problem Statement
In this lab section, you are going to learn how to read data from keyboard and write the data to a disk file. You are going to write an application that has one class (simple enough). One requirement for this lab is to use the following classes: BufferedReader, InputStreamReader, System.in, FileWriter (cannot use Scanner class).
This application will display the following message in the DOS console:
“Enter a line of text (‘stop’ to quit), then hit enter.)
: “
prompting the user type a line of string. The program will keep offer column symbol “: “ until the user typed ‘stop’. For each line typed, the program will write it to a disk file named “myData.txt” (make sure that each new line is stored as separate line). The disk should be automatically closed when the program exits.
Please follow the following sequence of steps to generate the content (Test Case 1).
Test Case 1
At the prompt type: the first line <enter>
At the prompt type: the second line <enter>
At the prompt type: this line contains 8, 9, 10 <enter>
At the prompt type: stop <enter>
Explanation / Answer
NOTE: Plese find the below code and let me know if you face any issues. I will revert back within 24 hours.
Code:
import java.io.*;
public class Assignment{
public static void main(String []args) throws IOException{
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
FileWriter fw = new FileWriter("myData.txt");
while(true){
System.out.println("Enter a line of text ('stop' to quit), then hit enter: ");
line = br.readLine().toLowerCase();
if(line.equals("stop"))
break;
else
fw.write(line+" ");
}
fw.close();
}
}
Execution output screenshot:
https://pasteboard.co/GALL2zi.png
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.