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

Using JAVA, read the following text file and write a new text file in the format

ID: 3805978 • Letter: U

Question

Using JAVA, read the following text file and write a new text file in the format shown below:

inputFile.txt

0
25 244
108 275
140 273
159 313
219 199
254 392
369 171
518 271
538 250
568 253
603 307
613 196
638 314

1
24 187
43 182
65 331
155 369


outputFile.txt

0,25,244
0,108,275
0,140,273
0,159,313
0,219,199
0,254,392
0,369,171
0,518,271
0,538,250
0,568,253
0,603,307
0,613,196
0,638,314
1,24,187
1,43,182
1,65,331
1,155,369

108 275 140 273 159 313 219 199 254 392 369 171 538 250 603 307 613 196 638 314 155 369 4533921103764 7219 4771997755091 8836 2223131222313 1133 5809949888338 4355 2045156136013 2465 111223555666 1

Explanation / Answer

ReadWriteFile.java

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


public class ReadWriteFile {

   public static void main(String[] args) throws FileNotFoundException {
       File inputFile = new File("D:\inputFile.txt");
       File outputFile = new File("D:\outputFile.txt");
       PrintWriter pw = new PrintWriter(outputFile);
       String s = "", str="";
       if(inputFile.exists()){
       Scanner inputScan = new Scanner(inputFile);
       while(inputScan.hasNextLine()){
           String line = inputScan.nextLine();
           if(!line.trim().equals("0") && !line.trim().equals("1") ){
               str =str+ s + ","+line.split("\s+")[0]+","+line.split("\s+")[1]+" ";
               pw.write(str);
               System.out.println(str);
               str="";
           }
           else{
               s = line;
           }
       }
       pw.flush();
       pw.close();
       inputScan.close();
       }
       else{
           System.out.println("inputFile.txt file does not exist");
       }
   }
  

}

Output:

0,25,244

0,108,275

0,140,273

0,159,313

0,219,199

0,254,392

0,369,171

0,518,271

0,538,250

0,568,253

0,603,307

0,613,196

0,638,314

1,24,187

1,43,182

1,65,331

1,155,369

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote