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

Here\'s what I have so far. I cant figure out how to get the output indented to

ID: 3685966 • Letter: H

Question

Here's what I have so far.

I cant figure out how to get the output indented to the rigth by 3 like it asks.

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

public class LineNumbers {

  
   public static void process(File input, File output){
       try{
           Scanner scan=new Scanner(input);
          
           PrintWriter print =new PrintWriter(output);
          
           int lineNumber=1;
           while(scan.hasNextLine()){
              
               String line=scan.nextLine();
               print.println(" "+lineNumber+" | "+line);;
               lineNumber++;
           }
           print.close();
           scan.close();
          
       }catch(IOException e){
          
       }
      
   }
}

Explanation / Answer

LineNumbers.java


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class LineNumbers {
   public static void main(String arg[]) throws Exception{
       String input = "D:\input.txt";
       String output = "D:\output.txt";
       File inputFile = new File(input);
       File outputFile = new File(output);
       if(inputFile.exists()){
           process(inputFile, outputFile);
       }
       else{
           System.out.println("File does not exist");
       }
   }
  
public static void process(File input, File output){
try{
Scanner scan=new Scanner(input);
  
PrintWriter print =new PrintWriter(output);
  
int lineNumber=1;
while(scan.hasNextLine()){
  
String line=scan.nextLine();
print.println(" "+lineNumber+" | "+line);;
lineNumber++;
}
System.out.println("Data has been wriitten in "+output.toString()+" and file generated successfully");
print.flush();
print.close();
scan.close();
  
}catch(IOException e){
  
}
  
}
}

Output:

Data has been wriitten in D:output.txt and file generated successfully

Note:

Change your input and output file path in below two variables in your program. Thats all you need to do, Program will exeute as it is.

String input = "D:\input.txt";
       String output = "D:\output.txt";