What I am trying to do is have it take a java file that look like this
public class test1 {
public static void main ( string[] args ) {
System.out.println( "This is Test 1." );
}
}
And it is suppose to output a text file with the proper spacing and indents. So far I can get the correct indenting for the first the lines. But I am having trouble with my second for loop that prints the spaces for the ending brackets. The ending brackets are prints outward like the first 3 lines instead of inward. Sorry if my variables are confusing.
Here is my code so far
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);
}
}
}
What it gives me is
public class Test1
{
public static void main( String[] args )
{
System.out.println( "This is Test 1." );
}
}
What I desire is this
public class Test1
{
public static void main( String[] args )
{
System.out.println( "This is Test 1." );
}
}