(Reformat Java source code) Write a program that converts the Java source code f
ID: 3804170 • Letter: #
Question
(Reformat Java source code) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For example, the following Java source in (a) uses the next-line brace style. Your program converts it to the end-of-line brace style in (b).
* Read the original Java source code from a text file with a ".java" extension; you will have to create and save this file manually (this file must be submitted)
* Process the source code as per the instructions in the textbook using String manipulation methods.
* Write the updated Java source code to a new text file also with a ".java" extension
Mublic class Test Mulic static voil Larin(Stringli args)Explanation / Answer
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class java.io.File { public static void main(String[] args) { if (args.length != 1) { System.out.println("Invalid argument."); System.out.println("Usage: java java.io.File Test.java"); System.exit(1); } File filename = new File(args[0]); if (!filename.exists()) { System.out.println(filename + " does not exist."); System.exit(2); } StringBuilder buffer = new StringBuilder(); try { Scanner input = new Scanner(filename); while (input.hasNext()) { String s = input.nextLine(); if (s.contains("{")) { buffer.append(" {"); } else { buffer.append(" " + s ); } } } catch (FileNotFoundException e) { e.printStackTrace(); } try { PrintWriter output = new PrintWriter(filename); output.write(buffer.toString()); output.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println(buffer); } public static int getIndex(String s, char ch) { for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.