Write the program in Java: Your assignment is to write a program that reads the
ID: 3684259 • Letter: W
Question
Write the program in Java:
Your assignment is to write a program that reads the attached text file and writes out a separate text file (using your first initial and last name as the filename). The new text file should contain all the same lines of text as the input file with the addition of a line number appended as the first character on the line.
Ex: if your input line reads:
this is a test
your output should read
1. this is a test
Chapter 11.txt File:
The first step to a career in technology is curiosity.
The next step is passion and drive.
The third step is accountability and humility - willingness to admit
that you can never possibly know everything; especially since it changes every second.
A final step is knowing how to communicate. Maybe you are the smartest person in the room,
but who cares if you cannot communicate your knowledge to others?
"Do or do not. There is no try" - yoda
Explanation / Answer
import java.util.Scanner;
import java.io.*;
public class ReadTextFile
{
public static void main (String [] args) throws IOException
{
int c=1;
File inFile = new File ("11.txt");
File outFile = new File ("xyz.txt");
FileWriter fWriter = new FileWriter (outFile);
PrintWriter pWriter = new PrintWriter (fWriter);
Scanner sc = new Scanner (inFile);
while (sc.hasNextLine())
{
String line = c+". "+sc.nextLine();
pWriter.println (line);
c++;
}
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.