[EDIT: this post was written before leppy's response. I thought I submitted this post but somehow it did not appear on the forum. The last sentence now doesn't apply anymore but imagine that chronologically I wrote this before leppy's response!]
[EDIT #2: I tested both closing and not closing the reader using System.exit(0) and not using it. In all cases System.exit(0) gave the exact same results as not using System.exit(0).]
My question about why things work the way they do (strings give an error even though String s = nextToken() is used, writer stores its data and releases it all at once instead of after every input). However, I have now found the part of my code and the code posted by Token several years ago, that was causing the issue.
For some reason, after not using the PrintWriter altogether, there was only one more thing causing the NZEC runtime error. Commenting out the line of code that closes the BufferedReader,
reader.close();
, instantly fixed the error and made SPOJ accept my program (that is, the one I used, which was created by Token, not me).
I'm relieved that things are working now for some unknown reason, but I'd still really appreciate to hear from anyone why these things work so strangely!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// import java.io.PrintWriter;
// import java.math.*;
// import java.util.*;
import java.util.StringTokenizer;
public class Main
{
private void solve() throws IOException
{
for (; ; )
{
int N = nextInt();
if (N == 42)
break;
System.out.println(N);
}
}
public static void main(String arg[])
{
new Main().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
// PrintWriter writer;
public void run()
{
try
{
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
// writer = new PrintWriter(System.out);
solve();
// reader.close();
// writer.close();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException
{
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException
{
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException
{
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}