Hi, this was my previous post https://www.daniweb.com/programming/software-development/threads/504670/need-help-with-a-java-program-that-will-indent-code-from-another-java-file#post2204763
I heard it would make more sense if I put all the code on one line and worked on it from there. I already have code that will indent properly, but I need code that will properly separate the text in Code.java
This is what Code.java looks like
// 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(); } }
I need it to look like this
// 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();
}
}
So I can plug that into my program so it will look like this
// 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();
}
}
How would I go about doing this?