Question: I can't figure out how to format this properly. Please check my current code. Here is my code so far: import java.util.Scanner; import java.io.File; import

 I can't figure out how to format this properly. Please check

I can't figure out how to format this properly. Please check my current code. Here is my code so far:

import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter;

public class Hw1_1 {

public static void main(String[] args) throws FileNotFoundException{ Scanner scan = new Scanner(System.in); System.out.println("Enter the name of the Java source code file:"); String fname = scan.nextLine(); try { Scanner in = new Scanner(new File(fname)); } catch (FileNotFoundException e) { e.printStackTrace(); } PrintWriter out = new PrintWriter(new File(fname + ".txt")); out.printf("%03d", ); //cannot figure out how to get this to print format specifed in question } }

Name your source code file Hwl_L.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 HwI_1java as the name of the input file and HwI_1.java contains 1I CLASS: Hw1_ (Hw1_1.java) public class Hw1_1 ( public static void main (String [] pArgs) public Hw1 1I then the contents of the output file HwI_1java.txt would be: [002] /7 CLASS: Hw11 (Hw1_1.java) [004] public class Hw1_1 ( [005] [006] [007] [008] [009]) public static void main (String[) pArgs) public w1_1f Hint: to print an integer as shown above in a field of width 3 with leading O's use the printf) method with a format specifier of %03d

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!