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;
}
}