Im making this program that reads in a java file and creates a new file with a formatted version of the file.
Basically every place there is a { open braces, I want to add 4 spaces to each lines after that etc..

Here is what I have so far..

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

public class Indent{
   public static int SPACES = 0;

    public static void main(String[]args)
         throws FileNotFoundException{
      Scanner sc = new Scanner(System.in);
      String fileN = setFile(sc);
      Scanner jfile = new Scanner(new File(fileN));
      String newTemp =
        fileN.substring(0,fileN.length()-5)+"Indented.java";
      PrintStream output = new PrintStream(new File(newTemp));
      while(jfile.hasNextLine()){
          String text = jfile.nextLine();
          fileFixed(text,output);  
      }
      System.out.println("done");
   }

   public static void fileFixed(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();
   }

public static String setFile(Scanner sc){
    System.out.print("File name: ");
      String input = sc.nextLine();
      File f = new File(input);
      while(!f.exists()||!input.endsWith(".java")){
         if(!f.exists()){
            System.out.println(input+" does not exist.");
         }
         if(!input.endsWith(".java")){
            System.out.println("File must be a Java source"+
                               " code file.");
        }
        System.out.print("File name: ");
        input = sc.nextLine();
        f = new File(input);
      }
      return input;
   }
  }

Recommended Answers

All 7 Replies

Do you have any questions or problems with the posted code?
Please explain.

sorry, I am wondering how I can make it add 4 spaces whenever data.next is a "{" and then when a "}" is next then I want to decrease the spaces by 4

so it will format basic java code spacing ...

When you read the next String following the "{", concatenate the spaces onto it:
line = " " + line; // add on 4 spaces

dosnt really help me much, yeah I know I need to add the spaces after eachline that comes after "{" but how can I do it?

how can I do it?

Not sure what you are asking. I posted code that shows how to do it.
When you find the line with the {, then use the above code to add 4 spaces to each of the following lines until it is time to stop adding the 4 spaces.

If you are having problems with the logic, stop and work out the logic in pseudo code before trying to write the code.

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

public class Indent{
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));
    String newTemp =
        fileN.substring(0,fileN.length()-5)+"Indented.java";
    PrintStream output = new PrintStream(new File(newTemp));
    while(jfile.hasNextLine()){
        String text = jfile.nextLine();
        if(text.contains("}")){
            SPACES-=4;
        }
        printLine(text,output);
        if(text.contains("{")){
            SPACES+=4;
        }
        for(int i = 0;i<SPACES;i++){
            output.print(" ");
        }
    }
    System.out.println("done");
}

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

}

//prompts user for filename and confirms its .java and exists
public static String setFile(Scanner sc){
    System.out.print("File name: ");
    String input = sc.nextLine();
    File f = new File(input);
    while(!f.exists()||!input.endsWith(".java")){
        if(!f.exists()){
            System.out.println(input+" does not exist.");
        }
        if(!input.endsWith(".java")){
            System.out.println("File must be a Java source"+
                               " code file.");
        }
        System.out.print("File name: ");
        input = sc.nextLine();
        f = new File(input);
    }
    return input;
}

}
`

currently it works fine with the open braces {, but when it checks for } it only changes the following lines but I need it to also perform the indentation on the line containing the }, any suggestions??

Look at the logic to see how to change the indent for the current line with the }

What does line 14 output?

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.