The error is your infinite loop. When you said,
There is no termination to the loop. The loop will always execute no matter what because:
1. The condition will always be true.
2. There is no breaking of the loop.
One way to fix this is by using a “break” (quotes for clarity) keyword. All this does is break out of the innermost loop that the break keyword is in.
So, your loop should be:
n = Integer.parseInt( br.readLine() );
if( n == 42 )
System.out.println( n );
What this loop does is it first gets the integer from the input and stores it as n. Then it checks if n is equal to 42. If n is equal to 42, the loop will be broken. This means that the computer will exit the loop and the loop will be terminated.
In general, I see a few things about your code than can be made better. I’ll list them out for you.
1. You're importing the Scanner class when it isn't being used at all in the program. It's just a waste of memory that way. You're better off leaving out that import statement.
2. You're throwing the NumberFormatException when it is not necessary. In general, it is good programming practice to be wary of these potential exceptions. But, for online competitions like CodeChef, you won't need it. You can safely assume that all the inputs they'll be giving are integers. You did nothing wrong. It's just unnecessary for this contest. If you were making a class for any other reason, it would be a good idea to throw the NumberFormatException; but you don't need to be that careful for CodeChef.
You need to keep IOException though. An IOException is possible while reading the input through the BufferedReader, so you still need to IOException to be thrown.
I also agree with maheshksd, your file name must be the same name as the name of your class in Java (There’s no good reason as to why aside from: It’ll give an error if you don’t). So if your class name is test, your file name should be test.java. If your file name is Test.java, TEST.java, or even tEst.java, it won’t work.
But the above statement is not true in only one scenario, which is solely for submissions on CodeChef. If you are submitting this program through the editor CodeChef provides, your class name MUST be “Main”(quotes for clarity). Otherwise, your program will not compile. This is because CodeChef takes your program and stores it in a .java file of its own known as Main.java.