So after scanning a file, I am to format it and output it following these guidelines:
-Lines are trimmed to remove any leading or trailing white space. Note that String has a
method trim() that removes leading/trailing white space.
A trimmed line is displayed with X leading spaces
The initial value for X is 0
If a ‘{‘ appears on a line then X is increased by 4 and this affects the display of subsequent
lines
If a ‘}’ appears on a line then X is decremented by 4 and this affects the display beginning
with the current line.
-The effect of this processing is similar to that done by the Auto-layout option in BlueJ.
My code so far is as follows:
public class PrettyPrint
{
public static void main (String[] args) throws IOException
{
String spaces = "";
Scanner kb = new Scanner(System.in);
System.out.println("Please enter the name of the file you wish to format");
String fileName = kb.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNextLine())
{
String line = inputFile.nextLine();
line.trim();
if (line.contains("{"))
{
spaces += " ";
line = spaces + line;
}
System.out.println(line);
}
}
}
The program is far from finished as you can see because I've run into a problem already. I was able to successfully add 4 spaces onto the line that contained the "{", however, it only affected that line alone. The rest of the lines after it had no added spaces. After looking back on the program I can see why. The issue is that I have no idea how to fix this. Any ideas?