Reformatting Java source code) Write a program that converts the Java source cod
ID: 3633970 • Letter: R
Question
Reformatting Java source code) Write a program that converts the Javasource code from the next-line brace style to the end-of-line brace
style. For example, the Java source here uses next-line brace style:
public class Test
{
public static void main(String[] args)
{
//Some statements
}
}
Your program should convert that code to end-of-line style:
public class Test {
public static void main(String[] args) {
//Some statements
}
}
Your program can be invoked from the command line with the Java
source code file as the argument. It converts the Java source code
to a new format. For example, the following command converts the
Java source code file Test.java to the end-of-line brace style:
java CodeConverter Test.java
Explanation / Answer
please rate - thanks
import java.util.*;
import java.io.*;
public class commandout
{
public static void main(String[] args)throws IOException
{String text;
boolean prev=false;
Scanner input=new Scanner(new File(args[0]));
while(input.hasNextLine())
{text=input.nextLine();
if(text.charAt(0)=='{')
{System.out.println("{");
prev=true;
}
else
{if(!prev)
System.out.println();
System.out.print(text);
prev=false;
}
}
input.close();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.