aeipownu
aeipownu

Reputation: 107

Java File processing. Basic. Error in text output

What I have to do is take a file which only has text in it and format it properly. Seems pretty easy in theory but I've run into a problem I'm not sure how to fix. Here is my code. TAB_SIZE is a final int set to 4.

    public static void justifyJava( String inputFileName,
                                    String outputFileName,
                                    int tabSize ) throws FileNotFoundException {

    Scanner input = new Scanner(new File(inputFileName));
    String str = "hi";
    int tabCount = 0;
    while (input.hasNext()){
        str = input.next();
        System.out.print(str + " ");
        if (str.equals("{")) {
           tabCount++;
           System.out.println();
           for (int i = 0; i < TAB_SIZE * tabCount; i++){
              System.out.print(" ");
           }
        } else if(str.equals(");")){
              tabCount--;
              System.out.println();
              for (int i = 0; i < TAB_SIZE * tabCount; i++){
                 System.out.print(" ");
              }
        } else if(str.equals("}")) {
              tabCount--;
              System.out.println();
              for (int i = 0; i < TAB_SIZE * tabCount; i++){
                 System.out.print(" ");
              }
        } 
    }    
}

So this code works great for a couple of the files I have to process but it gets stuck when there are 2 for loops in a row.

class Test3 { 
    public static void main( String[ ] args ) { 
        for( int i = 1; i < 10; i ++ ) { 
            System.out.println( "Hello World!" ); 
        } 
    for( int i = 1; i < 10; i ++ ) { 
        for( int j = 1; j < 10; j ++ ) { 
            for( int k = 1; k < 10; k ++ ) { 
                System.out.println( "Hello World!" ); 
            } 
        } 
    } 
if( 3 < 5 ) { 
     System.out.println( "Hello World!" ); 
} 
else if ( 4 < 5 ) { 
System.out.println( "Hello World!" ); 
if( 5 > 3 ) { 
System.out.println( "Hello World!" ); 
} 
} 
} 
} 

I understand that the problem is that after the "}" it is reducing the number of spaces but how can I get it to check if the next token is "for(" without the scanner actually moving to "for(" so it will still print when I run through again? Would I have to build a new scanner or reorganize code? Any help is appreciated.

Upvotes: 2

Views: 127

Answers (1)

Murthy
Murthy

Reputation: 387

Modified justifyJava() method to format properly and tried it with the Test3 class which is working properly, let me know if you find any difficulty:

public static void justifyJava(String inputFileName, String outputFileName,
        int tabSize) throws FileNotFoundException {

    Scanner input = new Scanner(new File(inputFileName));
    String str = "hi";
    int tabCount = 0;
    String prevToken = "";

    while (input.hasNext()) {
        str = input.next();         
        if(str.equals("{") || str.equals(");")){
            System.out.print(str + " ");
            System.out.println();
        }
        if(prevToken.equals(");") && !str.equals("}")){
            for (int i = 0; i < TAB_SIZE * tabCount; i++) {
                System.out.print(" ");
            }
        }else if (str.equals("{")) {
            tabCount++;
            for (int i = 0; i < TAB_SIZE * tabCount; i++) {
                System.out.print(" ");
            }
        } else if (str.equals("}")) {
            tabCount--;
            if(!");".equals(prevToken)){
                System.out.println();
            }
            for (int i = 0; i < TAB_SIZE * tabCount; i++) {
                System.out.print(" ");
            }
            System.out.print(str + " ");
        }
        if(!(str.equals("{") || str.equals(");") || str.equals("}"))){
            System.out.print(str + " ");
        }
        if((str!= null && !"".equals(str.trim()))){
            prevToken = str;
        }
    }
}

Upvotes: 1

Related Questions