Part III Java Source Code Reformatter 4 points Filenames Ref

Part III: Java Source Code Reformatter (4 points) Filename(s): ReformatCode.java
Public class: ReformatCode
Package-visible class(es): none

Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args[0] and the name of the file to save the formatted code in as args[1]. The original file is left untouched. The program makes no other changes the source code, including whitespace.

For example, the following command converts the Java source code file Test.java to the end-of-line brace style.

CSE 114 – Fall 2016 Homework #7 Page 3

}
The above source code would be reformatted into the following code, which is then saved in Test2.java:

}

Solution


package reformatcode;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;


public class ReformatCode {

public static void main(String[] args) throws Exception {

if(args.length<3)
throw new Exception(\"Insufficient number of commandline arguments\");
//open input file
Scanner infile=new Scanner(new FileReader(args[1]));
String lines[]=new String[100];
int n=0;
//reading file contents to an array
while(infile.hasNextLine())
{
lines[n++]=infile.nextLine();
}
//open output file
PrintWriter pw=new PrintWriter(new FileWriter(args[2]));
//reformatting
for(int i=0;i<n-1;i++)
{
if(lines[i+1].equals(\"{\"))
{
lines[i]= lines[i].concat(\" {\");
lines[i+1]=\"\";
pw.write(lines[i]);
i++;
}
else
pw.write(lines[i]);
  
}
}
  
}

Part III: Java Source Code Reformatter (4 points) Filename(s): ReformatCode.java Public class: ReformatCode Package-visible class(es): none Write a program that

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site