The following code writes 2 lines of text to a text file. I\'msupposed to fill i
ID: 3613032 • Letter: T
Question
The following code writes 2 lines of text to a text file. I'msupposed to fill in the missing code fragments so that itsuccessfully writes the indicated churchill string to a file calledelAlamein.txt, and create fileOut so that the new data overwritesany previous data in an existing file having the specifiedname.<fragment>
public class TextWriter
{
public static void main(String[] args)
{
String[] churchill =
{"Before Alamein we never had avictory.",
"After Alamein we never had adefeat."};
PrintWriter fileOut;
try
{
<fragment>
for (String line :churchill)
{
<fragment>
}
<fragment>
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
} // end main
} // end TextWriter class
For the 4 missing fragments I wrote:
Fragment 1:
import java.util.Scanner;
import java.io.*;
Fragment 2:
System.out.print("Enter filename: ");
fileOut = new PrintWriter(stdIn.nextLine());
fileOut.println(churchill);
Fragment 3:
line = fileOut.nextLine();
System.out.println(line);
Fragment 4:
fileOut.close();
I also wrote above String[] churchill that:
Scanner stdIn=new Scanner(System.in);
Explanation / Answer
please rate - thanks you have a few problems create fileOut so that the new data overwrites any previous data inan existing file having the specified name what new data--from the code it appears to be data youinput if you put your code together you get import java.util.Scanner; import java.io.*; public class TextWriter { public static void main(String[] args) {Scanner stdIn=new Scanner(System.in); String[] churchill = {"Before Alamein wenever had a victory.", "After Alamein we neverhad a defeat."}; PrintWriterfileOut; try { System.out.print("Enter filename:"); //are you inputting filename elAlamein.txt?? fileOut = new PrintWriter(stdIn.nextLine()); fileOut.println(churchill); for (Stringline : churchill) { line = fileIn.nextLine(); //fileIn not defined System.out.println(line); } //should you be writting this to a file--seeabove fileOut.close(); } catch (FileNotFoundExceptione) { System.out.println(e.getMessage()); } } // end main } // end TextWriter class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.