Reputation: 209052
I am teaching myself programming with Java through a textbook. An exercise asks you to write a program that reformats an entire source code file (using the command line):
from this format (next-line brace style):
public class Test
{
public static void main(String[] args)
{
// Some statements
}
}
to this format (end-of-line brace style):
public class Test {
public static void main(String[] args) {
// Some statements
}
}
Here's the code I have so far:
import java.io.*;
import java.util.*;
public class FOURTEENpoint12 {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println
("Usage: java FOURTEENpoint12 sourceFile targetFile");
System.exit(1);
}
File sourceFile = new File(args[0]);
File targetFile = new File(args[1]);
if (!sourceFile.exists()) {
System.out.println("File does not exist");
System.exit(2);
}
Scanner input = new Scanner(sourceFile);
PrintWriter output = new PrintWriter(targetFile);
String token;
while (input.hasNext()) {
token = input.next();
if (token.equals("{"))
output.println("\n{\n");
else
output.print(token + " ");
}
input.close();
output.close();
}
}
I have two problems:
I have tried many different while blocks, but can't get anything close to what the book is asking. I've tried different combinations of regular expressions, delimeters, token arrays, but still way off.
The book asks you to reformat a single file, but the chapter never explained how to do that. It only explains how to rewrite the text into a new file. So I just wrote the program to create a new file. But I would really like to do it the way the problem asks.
If someone could help me with these two problems, it would actually solve many of the problems I'm having with alot of the exercises. Thanks in advance.
Upvotes: 2
Views: 1185
Reputation: 9
import java.io.*;
import java.util.Scanner;
public class ReformatinCode {
public static void main(String[] args) throws Exception{
File f1 = new File ("java8_19.txt");
File f2 = new File("java8_19_result.txt");
Scanner scr1 = new Scanner(f1);
StringBuffer sb1 = new StringBuffer();
PrintWriter pw1 = new PrintWriter(f2);
while(scr1.hasNext()){
sb1.append(scr1.nextLine() + System.lineSeparator());
}
scr1.close();
String output = format(sb1.toString());
System.out.println(output);
pw1.print(output);
pw1.close();
}
public static String format(String s){
String result ="";
result = s.replaceAll("\\\r\\n\\s\\{","\\{");
return result;
}
}
/* this is a working answer , notice that the code that needed to be reformated is stored in the file "java8_19.txt"... and the file "java8_19_result.txt" will be created after running the code.... both will be in the workspace directory within the package folder
Upvotes: 0
Reputation: 31245
This is feasible with a simple regular expression.
You want to replace ) *any whitesplace character* {
with ) {
.
In java regex, there is a predefined regex : \s
which matches all whitespace characters : [ \t\n\x0B\f\r]
Here, you want to replace )\s*{
with ) {
.
To achieve this, load the whole file in a String (called input
below) and use String#replaceAll(String regex, String replacement) :
//Added the escape characters...
input.replaceAll("\\)\\s*\\{", ") {");
Test :
String input = "public static void main() \n {";
System.out.println(input.replaceAll("\\)\\s*\\{", ") {"));
Output :
public static void main() {
Upvotes: 1
Reputation: 109597
First: hasNextLine()
and nextLine()
seem more appropiate for reading line wise.
And then consider what has to be done.
Read:
line = " public static void main(String[] args)";
Read:
line = " {"
Write:
public static void main(String[] args) {
And everything else has to be copied.
Maybe you want to print(line)
instead of println(line)
and separately determine a println()
? Maybe you need to remember what you just did boolean linePrintedWithoutLn = false;
.
Upvotes: 0
Reputation: 62858
If I read your question correctly, all you need is keeping track of previous line, and current line. Since you're learning, I'll just give you rough outline:
If current line contains just brace and whitespace, then forget the current line, add space and brace to previous line, and keep it as previous line.
Else write previous line to output, set current line to be the previous line.
Read new current line and repeat above, until end of input file.
Try changing your code to match that, reading line by line.
You can detect if line is {
either by trimming and comparing to "{"
, or with regexp like
"^\\s*\\{\\s*$"
(untested).
Note: this is the simplest possible version, which assumes the movable is brace alone on a line.
Upvotes: 1