Line indentor - Length of java between classes being set to 0 : I have the task of making an indentor for some javaline. The idea is that first I have to split the java from the comment. I then have to find the longest line and use that for the max length of java. However I have run into the issue of that its returning with 0 every time and not the longest line in an array.

The first piece of code is the class that holds the method for working out the longest length of javaline. The method is called findMaxJavaLineLength():

    
    import java.util.*;

    class Program
    {

    private ArrayList<JavaLine> stored = new ArrayList<>();

    /**
     * Constructor
     */
    public Program()
    {
        stored.clear();
    }

    /**
     * Add the next line of Java code to the stored program
     * @param line A line of Java code
     */
    public void addLine( String line )
    {
        stored.add( new JavaLine( line) );
    }

    public String indentProgram()
    {
        String res = "";

        for ( JavaLine line: stored )
        {
            res += line.returnLineWithCommentAt() + "\n";
        }
        return res;

    }

    public int findMaxJavaLineLength()
    {
        int max = 0;

        for ( JavaLine line: stored )
        {
            int lineLength = line.getJavaLineLength();
            if ( lineLength > max) max = lineLength;
        }
        return max;
    }

    }

and the second piece of code is the JavaLine class which splits the code into parts and returns the length of java:


    class JavaLine
    {
    private String java    = ""; // Java code on line 
    private String comment = ""; // The single line comment
    private String javaSpace = "";
    private int lenJava = 0;  // The line length of just the java cod

    public JavaLine( String line )
    {
        String[] splitted = line.split("// ");
        java = splitted[0];
        int slashCount = line.length() - line.replaceAll("//","").length();
        if (slashCount >= 1) {
            comment = "// " + splitted[1];
        }

    }

    public int getJavaLineLength()
    {

        lenJava = java.length();
        return lenJava;

    }

    public String returnLineWithCommentAt( )
    {
        String output = "";
        Program t = new Program();
        int maxLenJavaLine = t.findMaxJavaLineLength();
        int number = maxLenJavaLine - getJavaLineLength();
        String javaSpace = "";

        for (int n = 0; n < number; n++){
            javaSpace = javaSpace + " ";
        }

        String commentOutput = comment;
        if ( java.contains("}")){
            output = java;
        }else if ( java.contains("{")){
            output = java;
        } else {
            output = java + javaSpace + commentOutput + "test: " + t.findMaxJavaLineLength();
        }

        return output;

    }
    }

I have tested it and it seems like the lenJava bit in the getjavalength is working out the length of the javaline correctly when I grab it from the same class. However it sets to 0 when I grab it from the other class when trying to work out the longest line. It seems as if the length just doesn't get passed to the other class from the javaline class.

Below is the output that I am currently receiving.

    import java.lang.Thread;// Use External classtest: 0
    class Countdowntest: 0
    {
     public static void main( String args[] )test: 0
     {
     // Start from 10test: 0
     while ( countdown > 0 )// While greater than 0test: 0
     {
     System.out.println(countdown);// Write contents of countdowntest: 0
     if ( countdown == 3 )// If equal to 3test: 0
     {
     System.out.println("Ignition");// Write Ignitiontest: 0
     }
     countdown--;// Decrement countdown by 1test: 0
     try {
     Thread.sleep( 100 );// 1000 milliseconds delaytest: 0
     }
     catch( InterruptedException e ) {}
     }
     System.out.println( "Blast Off");// Write Blast offtest: 0
     }
    }

The bit of the output where it says "test: 0" is where I am expecting the length of the javaline which is the longest. so it should be "test: 50" or whatever instead of "test: 0".

Thank you

