我想要做的是让它看起来像这样的java文件
public class test1 {
public static void main ( string[] args ) {
System.out.println( "This is Test 1." );
}
}
并且假设输出具有适当间距和缩进的文本文件 . 到目前为止,我可以获得第一行的正确缩进 . 但是我在第二个for循环中遇到了麻烦,它打印了结束括号的空格 . 结束括号像前三行而不是向内打印 . 对不起,如果我的变量令人困惑 .
到目前为止,这是我的代码
public class JavaJustifier {
public static void main( String[] args )
throws FileNotFoundException {
for( int i = 1; i < 6; i++ ) {
justifyJava( "Test" + i + ".java",
"Justified" + i + ".txt",
4 );
}
}
public static void justifyJava( String inputFileName,
String outputFileName,
int tabSize )
throws FileNotFoundException {
int counter = 0;
int counter2 = 0;
int blah = 0;
File f = new File(inputFileName);
File p = new File(outputFileName);
if (p.exists())
p.delete();
Scanner input = new Scanner (f);
PrintStream name = new PrintStream(new File(outputFileName));
while (input.hasNextLine()) {
String line = input.nextLine();
Scanner lineScan = new Scanner(line);
if (line.contains("{") == true) {
name.print("{\r\n");
counter++;
for (int i = 1; i <= counter; i++) {
for (int j = 0; j <= tabSize; j++) {
name.print(" ");
}
}
System.out.println(counter);
} else if (line.contains("}") == true) {
name.print("\r\n");
counter--;
for (int x = 1; x <= counter; x++) {
for (int y = 1; y <= tabSize; y++) {
name.print(" ");
}
}
name.print("}");
System.out.println(counter);
} else {
name.print(line);
}
}
}
它给我的是什么
public class Test1
{
public static void main( String[] args )
{
System.out.println( "This is Test 1." );
}
}
我想要的是这个
public class Test1
{
public static void main( String[] args )
{
System.out.println( "This is Test 1." );
}
}
1 回答
好吧,我已经重构了一些东西(这是必要的),但它的工作方式与你期望的完全相同(AFAIK!) .
这是代码: