简体   繁体   中英

Java: Counter Indentation

I am very new to java and trying to create a java app which (when ran inside a terminal) will copy what text is inside and if there is a curly { bracket then add 3 spaces, when there is a curly } bracket then remove 3 spaces. There should be a counter to indent another 3 spaces each time a { appears (see example)

Example: File1.txt :

Hello{StackOverflow}{Users}

The output should be File2.txt :

Hello
{
   StackOverflow
}

{
      Users
}

What I currently get outputed to File2.txt is:

Hello
{
   StackOverflow
}

{
   Users

I am missing my last bracket (how do I fix this?) and don't know how to loop my indentation based on the counter. Please help

My current code:

import java.io;
import java.util.Scanner;

public class myapp {

    public static void main(String[] argv) throws IOException {

        File InputFile = new File(argv[0]);
        Scanner FileScanner = new Scanner(InputFile);
        FileWriter Writer = new FileWriter(argv[1]);
        BufferedWriter OutputWriter = new BufferedWriter(Writer);

        while (FileScanner.hasNextLine() == true) {
            String a = FileScanner.nextLine();

            try {
                int indent = 0;
                {

                    if (a.contains("{")) {
                        indent++;

                    }

                    for (int i = 0; i < indent; i++) {

                        OutputWriter.write("   ");
                    }
                    OutputWriter.write(a);
                }

                if (a.contains("}")) {
                    indent--;

                }

            } catch (Exception e) {
                System.out.println("Error:" + e.getMessage());
            }
            OutputWriter.write("}");
        }
    }
}

ps in Terminal (to run/test) I use the following command:

$java myapp File1.txt File2.txt

Thank you :)

Try closing OutputWriter at the end of the method via OutputWriter.close() . It will flush the stream which is likely the cause of the missing } .

As far as your indentation issue, declare and initialize the indent counter outside the loop. Otherwise, it will get reset to 0 with each iteration.

Try this it's ok I add the finally block.`

        finally{
            OutputWriter.close();
        }

Try this one:

public static void main(String[] args) throws IOException {
 File InputFile = new File(argv[0]);
 Scanner FileScanner = new Scanner(InputFile);
 FileWriter Writer = new FileWriter(argv[1]);
 BufferedWriter OutputWriter = new BufferedWriter(Writer);
 while (FileScanner.hasNextLine() == true) {
  String a = FileScanner.nextLine();
  String b = "";
  for (int i = 0; i < a.length(); i++) {
   if (a.charAt(i) == '{') {
     b += "\n{\n   ";
   } else if (a.charAt(i) == '}') {
     b += "\n}\n";
   } else {
     b += a.charAt(i);
   }
}
OutputWriter.write(b);
OutputWriter.close();
}}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM