whats the error??

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

class test {

	public static void main(String[] args) throws NumberFormatException, IOException {

		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		int n=0;
		int k = 0;
		while(true)
		{
			n=Integer.parseInt(br.readLine());
			
			if(n==42)
				k=1;
			if(k!=1)
			System.out.println(n);
			
		}
		
	}

}

I wrote this program for the program with code TEST but it says a run time error everytime?
how do I fix it??

Your program does not terminate due to the while(true){… } loop. It merely stops printing numbers once 42 is encountered, but it keeps running forever. So what happens it asks to input more numbers even after the end of the input file, leading to runtime error. To resolve this, terminate your program on reaching the EOF(end of file).

First reason for run time err may you are not using same file name and class name .
in your case use test.java , its better to use Class name beginning with caps like Test and file name Test.java

 Then change your code as below 

import java.io.BufferedReader; 

import java.io.IOException; 

import java.io.InputStreamReader;
 
import java.util.Scanner;

class test {

public static void main(String[] args) throws NumberFormatException, IOException {

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    int n=0;
    int k = 0;
    while(true)
    {
        n=Integer.parseInt(br.readLine());

        if(n==42)
            break;
        if(k!=1)
        System.out.println(n);

    }

}

}

The error is your infinite loop. When you said,

while(true) {...}

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:

while(true) {

    n = Integer.parseInt( br.readLine() );

    if( n == 42 )
        break;
    else
        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.