There is no way to tell how large the input set will be. Your recursive algorithm will go as many levels deep as it can until it runs out of memory. I would suspect this as the largest culprit of your NZEC.
You can use the same algorithm as you have here iteratively by using a while loop instead of recursing. This has the added benefit of not requiring O(number of inputs) memory via recursion and instead uses O(1) memory.
public class LifeUniverseEverything {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n != 42) {
System.out.println(n);
n = sc.nextInt();
}
}
}