Hi, so I'm trying to create a Java program that will read a Java file that is incorrectly formated and properly indent and format it.

This is what I have so far.

import java.io.*;
import java.util.*;

public class Project3 {
    public static int SPACES = 0;

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String fileN = setFile(sc);
        Scanner jfile = new Scanner(new File(fileN));
        while (jfile.hasNextLine()) {
            String text = jfile.nextLine();
            if (text.contains("}")) {
                SPACES -= 4;
            }
            for (int i = 0; i < SPACES; i++) {
                System.out.print(" ");
            }
            printLine(text);
            if (text.contains("{")) {
                SPACES += 4;
            }
        }
    }

    public static void printLine(String text) {
        Scanner data = new Scanner(text);
        while (data.hasNext()) {
            System.out.print(" " + data.next());
        }
        System.out.println();
    }

    public static String setFile(Scanner sc) {
        System.out.println("Enter file to read");
        String input = sc.nextLine();
        File f = new File(input);
        while (!f.exists() || !input.endsWith(".java")) {
            if (!f.exists()) {
                System.out.println("Does not exist");
            }
            if (!input.endsWith(".java")) {
                System.out.println("Cannot read");
            }
            System.out.print("Enter file to read");
            input = sc.nextLine();
            f = new File(input);
        }
        return input;
    }
}

This is my output.

 // Code.java
 import java.io.*;
 import java.util.*;
 public class Code {
     public static void main(String[] args) throws FileNotFoundException {
         Scanner input = new Scanner(new File("words.txt"));
         PrintStream output = new PrintStream(new File("words2.txt"));
         while (input.hasNextLine()) {
             String text = input.nextLine();
             echoFixed(text, output);
             echoFixed(text, System.out);
             input.close();
         }
     }
     public static void echoFixed(String text, PrintStream output) {
         Scanner data = new Scanner(text);
         if (data.hasNext())
         {
             output.print(data.next());
             while (data.hasNext()) {
             output.print(" " + data.next());}}
             output.println(); data.close();
         }
     }

This is the output that I want to get

// Code.java
import java.io.*;
import java.util.*;

public class Code {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("words.txt"));
        PrintStream output = new PrintStream(new File("words2.txt"));
        while (input.hasNextLine()) {
            String text = input.nextLine();
            echoFixed(text, output);
            echoFixed(text, System.out);
            input.close();
        }
    }

    public static void echoFixed(String text, PrintStream output) {
        Scanner data = new Scanner(text);
        if (data.hasNext()) {
            output.print(data.next());
            while (data.hasNext()) {
                output.print(" " + data.next());
            }
        }
        output.println();
        data.close();
    }
}

And this is what the Java file that I am working with.

// Code.java
import java.io.*;
import java.util.*;
public class Code {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("words.txt"));
PrintStream output = new PrintStream(new File("words2.txt"));
while (input.hasNextLine()) {
String text = input.nextLine();
echoFixed(text, output);
echoFixed(text, System.out);
input.close();
}
}
public static void echoFixed(String text, PrintStream output) {
Scanner data = new Scanner(text);
if (data.hasNext()) 
{ 
output.print(data.next());
while (data.hasNext()) {
                output.print(" " + data.next());}}
        output.println(); data.close();
        }
        }

My code works until it gets to the second method, there's some problems with the indentations but I'm not sure what's going on. My code also prints out a space before every line, but I'm not sure why it's doing that.

Recommended Answers

All 9 Replies

  1. What is the printLine method for? Why not just print the text?
  2. The printLine method is where you always print a blank before every line
  3. Your code does not handle the case where there is more than one } or more than one { on any one line.

This looks to me to be another "Pretty Printer" or code beautifier. There are now many old discussions about this plus open source efforts.

Sometimes it's best to use a wheel than create a new one?

That depends on whether the objective is to get a pretty printer, or to complete a learning exercise that just happens to be a p.p.
I don't know which this is

@JamesCherrill
1. I tried printing out the text with System.out.println but I ran into some issues with some indentations with it so the printLine method is what my friend and I came up with.
2. Thanks for pointing that out!
3. Oh I see, I guess that's why there's errors in the second method. How would I go about doing this? Is there a way to do .contains, but have it check for multiple braces on a line?

@rproffitt
Hmmmm, I did not know about this, thanks for telling me about it, I'll go check it out!

Astyle Click Here looks nice. It is even included in ubuntu linux (but it is not coded in java!).

@Gribouillis
Thanks, but I'm looking for something coded in Java

Instead of contains you can use indexOf(int ch, int fromIndex) in a tiny loop to count the number of { or } in each line.

...

or simply loop thru all the chars in the String testing for { or } and incrementing/decrementing the indent level as yo go

@JamesCherrill

So, I'm guessing the same concept works for checking for multiple semicolons right?
Also, how would I use indexOf if every line has a different amount of characters?

Something like

count = 0, startIndex = 0;
while ((startIndex = string.indexOf(testChar, startIndex) >= 0) count++;

.. for each char you are interested in, but I prefer the loop thru all characters ...

for (char c : string.toCharArray()) {
    switch (c) {
        case '{'   indent++
        case '}'   indent--
        case ';' etc

(all code above is pseudocode / simplified Java)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.