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.