Printing to Standard Output in java is causing NZEC error for the problem May 2014 CHEFBM

I solved the problem http://www.codechef.com/MAY14/problems/CHEFBM/ but when I print to console using System.out.println is causing NZEC runtime error.

If I comment out System.out.println statements then I am getting wrong answer as it’s expected.

Total time for wrong answer is 0.06 but when I used the standard output the time is taking 0.66 and the program is failing with runtime error NZEC

1 Like

For sure the problem is not in sysout, but in what you are printing and it can be caused by a lot things…

For example:

ArrayList list = null;
System.out.println( list.get(0) ); // NPE -> NZEC

I rechecked all the possibilities for the program I have written

At the first of the main function I redirected the STDOUT to another buffer

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
PrintStream old = System.out;
System.setOut(ps);

and at last of the main function

System.out.flush();
System.setOut(old);
String output = baos.toString();
System.out.print(output);
System.out.flush();

If I comment out the line System.out.print(output); then my program is giving wrong answer.

If the print statement System.out.print(output); is there then the program is failing with the runtime exception.

I think there is some issue with Standard Output of CodeChef system.

And any reason why are you using it so?

ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); PrintStream old = System.out; System.setOut(ps);

I’m using simple System.out.println()

And if the output is bigger I print everything to StringBuilder and later in one statement I call System.out.println(buff.toString()) again.