Write a program that reads strings from the user and writes them to an output fi
ID: 3687752 • Letter: W
Question
Write a program that reads strings from the user and writes them to an output file called userStrings.txt. Stop processing when the user enters the string "DONE". Do not write the sentinel string ("DONE") to the output file.
Please use this to base your response off of:
import java.util.Random;
import java.io.*;
public class TestData
{
//-----------------------------------------------------------------
// Creates a file of test data that consists of ten lines each
// containing ten integer values in the range 10 to 99.
//-----------------------------------------------------------------
public static void main (String[] args) throws IOException
{
final int MAX = 10;
int value;
String file = "test.dat";
Random rand = new Random();
FileWriter fw = new FileWriter (file);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter outFile = new PrintWriter (bw);
for (int line=1; line <= MAX; line++)
{
for (int num=1; num <= MAX; num++)
{
value = rand.nextInt (90) + 10;
outFile.print (value + " ");
}
outFile.println ();
}
outFile.close();
System.out.println ("Output file has been created: " + file);
}
}
Explanation / Answer
sample code-
import java.io.*;
import java.io.IOException;
public class classname
{
public static void main(String[] args) {
String newLine = System.getProperty("line.separator");
System.out.println("Reading Strings from console");
try
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String userInput;
System.out.println("Enter text...");
System.out.println("Enter 'quit' to stop entering.");
do {
userInput = (String) br.readLine();
System.out.println("You entered : " + userInput);
}
while(!userInput.equals("quit"));
//read a line from the console
String lineFromInput = br.readLine();
//create an print writer for writing to a file
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
//output to the file a line
System.setOut(out);
//close the file
out.close();
}
catch(Exception e)
{
System.out.println("Error during reading/writing"); }
}
}
note-by using the above code with required modifications the given question can be answered.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.