Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Name your source code file Hw1_1.java. Write a program that prompts the user for

ID: 3844501 • Letter: N

Question

Name your source code file Hw1_1.java. Write a program that prompts the user for the name of a Java source code file. The program shall read the source code file and output the contents to a new file named the same as the input file, but with a .txt file name extension (e.g., if the input file is foo.java then the output file shall be named foo.java.txt). Each line of the input file shall be numbered with a line number (formatted as shown below) in the output file. For example, if the user enters Hw1_1.java as the name of the input file and Hw1_1.java contains:

//*************************************************************** // CLASS: Hw1_1 (Hw1_1.java) //***************************************************************

public class Hw1_1 { public static void main(String[] pArgs) { } public Hw1_1() { } } then the contents of the output file Hw1_1.java.txt would be:

[001] //***************************************************************

[002] // CLASS: Hw1_1 (Hw1_1.java)

[003] //***************************************************************

[004] public class Hw1_1 {

[005] public static void main(String[] pArgs) {

[006] }

[007] public Hw1_1() {

[008] }

[009] }

Hint: to print an integer as shown above in a field of width 3 with leading 0's use the printf() method with a format specifier of %03d

Explanation / Answer

Code:

import java.util.*;
import java.io.*;

public class Hw1_1{
public static void main(String []args){
// Declaring the required variables
Formatter fmtFile;
String filename, line;
int num = 1;
  
// Using scanner to read the input file name
Scanner sc = new Scanner(System.in);
System.out.println("Enter the filename: ");
filename = sc.nextLine();
  
try{
// Bufferred reader to create buffered stream for the given file
BufferedReader reader = new BufferedReader(new FileReader(filename));
fmtFile = new Formatter(new FileOutputStream(filename+".txt"));
// Reading each line from file and writing to the same filename but with .txt extension
while ((line = reader.readLine()) != null){
fmtFile.format("[%03d]%s ", num++, line);
}
// closing the input and output readers
reader.close();
fmtFile.close();
}
// catching the error in case of any exceptions
catch (Exception e){
System.err.format("Exception occurred while trying to read file" + filename);
e.printStackTrace();
}
}
}

Execution and output:
dev-dsk-bonkv-1b-773d1a8c % javac Hw1_1.java

(17-05-28 1:46:54) <0> [~]
dev-dsk-bonkv-1b-773d1a8c % java Hw1_1
Enter the filename:
Hw1_1.java

dev-dsk-bonkv-1b-773d1a8c % cat Hw1_1.java.txt
[001]import java.util.*;
[002]import java.io.*;
[003]
[004]public class Hw1_1{
[005] public static void main(String []args){
[006] // Declaring the required variables
[007] Formatter fmtFile;
[008] String filename, line;
[009] int num = 1;
[010]
[011] // Using scanner to read the input file name
[012] Scanner sc = new Scanner(System.in);
[013] System.out.println("Enter the filename: ");
[014] filename = sc.nextLine();
[015]
[016] try{
[017] // Bufferred reader to create buffered stream for the given file
[018] BufferedReader reader = new BufferedReader(new FileReader(filename));
[019] fmtFile = new Formatter(new FileOutputStream(filename+".txt"));
[020] // Reading each line from file and writing to the same filename but with .txt extension
[021] while ((line = reader.readLine()) != null){
[022] fmtFile.format("[%03d]%s ", num++, line);
[023] }
[024] // closing the input and output readers
[025] reader.close();
[026] fmtFile.close();
[027] }
[028] // catching the error in case of any exceptions
[029] catch (Exception e){
[030] System.err.format("Exception occurred while trying to read file" + filename);
[031] e.printStackTrace();
[032] }
[033] }
[034]}
[035]

(17-05-28 1:48:36) <0> [~]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote