0

Under normal conditions the following code:

String[] outputStrings =  {" Initial values: a = 1, b = 2 ", "Swapped values: a = 2, b = 1", ""};

System.out.println("array length: " + outputStrings.length);

int i = 0;
for (String line: outputStrings) {
    String str = Integer.toString(i) + " : |" + line + "| ";
    System.out.println(str);
    //System.out.flush();
    i++;
}

Has the following output:

array length: 3
0 : | Initial values: a = 1, b = 2 | 
1 : |Swapped values: a = 2, b = 1| 
2 : ||

However, the following code:

CommandLineTestScaffolding scaffolding;
scaffolding = new CommandLineTestScaffolding(ExchangeCL::main);

String[] inputStrings = {"1", "2"}; // string literals with integer numbers
String[] outputStrings = scaffolding.run(inputStrings);

scaffolding = null; //no longer necessary

System.out.println("array length: " + outputStrings.length);

int i = 0;
for (String line: outputStrings) {
    String str = Integer.toString(i) + " : |" + line + "| ";
    System.out.println(str);
    //System.out.flush();
    i++;
}

Has the following strange output:

array length: 1
0 : | Initial values: a = 1, b = 2 
Swapped values: a = 2, b = 1
| 

For information, the CommandLineTestScaffolding is a simple contraption to temporary redirect System.out to fetch command line output to an array of strings:

public class CommandLineTestScaffolding {

    private Consumer<String[]> theMethod; //Place to store main method generating text output

    public CommandLineTestScaffolding(Consumer<String[]> method) {

        theMethod = method;
    }

    public String[] run(String[] args) {

        // Create a stream to hold the output
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);

        // A variable to temporarily hold System.out stream
        PrintStream oldOutputStream = null;

        // Critical section. System.out must be restored to its original value
        try {

            System.out.flush(); // Clear buffer;

            // IMPORTANT: Save the old System.out!
            oldOutputStream = System.out;

            // Use new wrapped byte array stream
            System.setOut(printStream);

            theMethod.accept(args); //Run the method. fill the stream

            printStream.flush(); // Clear buffer;

        }
        finally {

            // Put things back
            System.setOut(oldOutputStream);

        }

        String[] output = outputStream.toString().split("/n"); //split into lines with regex
        printStream = null;
        outputStream = null;
        return output;
    }

}

What leads to such strange behaviour?

Bashir
  • 2,057
  • 5
  • 19
  • 44
Pavlo Maistrenko
  • 187
  • 2
  • 12
  • 2
    Split on `\n` and not `/n` – pafau k. Jun 03 '20 at 10:18
  • Thank you! I found that to account both for UNIX/OSX and Win style of newline .split("(\\r?\\n)", -1) works. See https://stackoverflow.com/questions/454908/split-java-string-by-new-line about CR and LF and https://stackoverflow.com/questions/14602062/java-string-split-removed-empty-values about retaining empty lines. – Pavlo Maistrenko Jun 03 '20 at 11:42

1 Answers1

-2

outputStrings has just one element, and it is "0 : | Initial values: a = 1, b = 2 Swapped values: a = 2, b = 1 | "

So, it is not strange behaviour.

Your CommandLineTestScaffolding does not do the job ypu expect.

However, i did not understand what you need? You need exact same output with 1?

  • Yes, I want the same output. – Pavlo Maistrenko Jun 03 '20 at 10:20
  • outputString.split("(\\r?\\n)", -1); does the job for me. – Pavlo Maistrenko Jun 03 '20 at 11:34
  • @PavloMaistrenko this answer didn't mention the sollution that fixed your problem (as I can see from the comment using split("(\\r?\\n)"), so why you accept that answer? you're making later user get confused with a non related answer also you're giving free reputation – Bashir Jun 03 '20 at 13:15
  • @Bashir, technically, the answer is not complete, but it gave me a hint enough to trace and solve the problem on my own. – Pavlo Maistrenko Jun 03 '20 at 14:17
  • 1
    @PavloMaistrenko simply the answer is the comments, I don't see no need to accept this answer. she didn't even understand what you need, as she said... whatever – Bashir Jun 03 '20 at 14:19
  • Yeah I think this can be a bit misleading for future visitors. Luckily this is not a very general question, so probably no harm done. – pafau k. Jun 03 '20 at 14:45
  • Ok, I think, I'd better leave this answer as accepted. Next time I will take more time before accepting answers. Thank you very much everyone! – Pavlo Maistrenko Jun 04 '20 at 16:33