Search code examples
javaarraylistconditional-statementsfilewriter

Printing a set of conditions between a specified number of elements


As part of a program that generates a text output file, I need to insert a predefined number n (which corresponds to the number of required elements) and automatically create conditions among them.

E.g. for n=3, the elements s1, s2 and s3 are generated. What I want to achieve is to generate the following conditions automatically and print them in a text file:

s1<s2  & s1<s3
s2<=s1 & s2<s3
s3<=s1 & s3<=s2

The above conditions cover all possible combinations between the elements. What I have so far is an ArrayList that holds as many elements as the predefined number n and the following code to produce the required results:

int counter = 0;
ArrayList<String> state = new ArrayList<String>();
for (int i=0; i<=n; i++) {
    state.add(i,"s"+1));
}

for (int j=1; j<=n; j++) {

    for (int i=0; i<(n-1); i++) {
        if (i==(n-2)) {
           fw.write(state.get(i)+"<"+state.get(counter));
        }
        else {
           fw.write(state.get(i)+"<"+state.get(counter)+" & ");
        }
    }
    counter++;
}

I know that using the counter in this case is not correct, but I cannot think of a way to correctly represent the different conditions.

UPDATE:

Using the following code for testing purposes I managed to get close to the desired outcome:

ArrayList<String> collectStates = new ArrayList<String>();
String activeState,writtenState;
for (int i=0; i<n; i++) {
        for (int j=0; j<state.size(); j++) {
            if (state.get(i) != state.get(j)) {
                activeState=state.get(i)+"<"+state.get(j);
                writtenState=state.get(j)+"<"+state.get(i);

                if (collectStates.contains(activeState)) {
                    System.out.print(state.get(i)+"<="+state.get(j));
                }
                else {
                    System.out.print(activeState);
                }
                if (j!=state.size()-1) {
                    System.out.print(" & ");
                }
                collectStates.add(writtenState);
            }
        }
        System.out.print("\n");
    }

The output I receive is:

s1<s2 & s1<s3
s2<=s1 & s2<s3
s3<=s1 & s3<=s2 & 

The only problem is the extra & at the end of the last line, which I don't know yet how to fix.


Solution

  • I found the answer to my question! In the updated section of the description I performed the following addition to remove the extra & symbol:

    Old version:

    if (j!=state.size()-1) {
        System.out.print(" & ");
    }
    

    Updated version:

    if ((j!=state.size()-1) && ((j!=state.size()-2) || (i!=state.size()-1))) {
        fw.write(" & ");
    }
    

    The problem appeared always in the last row of the output conditions, thus the newest fix. The output is now exactly as described in the description.

    In further detail, what the program did is to remove the identical values:

    s1?s1 & s1?s2 & s1?s3
    s2?s1 & s2?s2 & s2?s3
    s3?s1 & s3?s2 & s3?s3
    

    So by removing:

    s1?s1 & we keep s1?s2 & s1?s3

    s2?s2 & we keep s2?s1 & s2?s3

    s3?s3 we keep s3?s1 & s3?s2 &

    As a result, the addition of the new condition is necessary to remove the extra & in the last row.