EOF in java?

How to input a string in java if we dont know the number of Testcases that is end of file?

1.Can be done by Scanner class.

while(sc.hasNext()){ //until EOF
int a=sc.nextInt()

 2. Using BufferedReader 

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string = null;
while((string = br.readLine())!= null){
// whatever to be done

 3. Using Try Catch

while(true) {
try {
//Input here via any mode of input
catch(Exception e){
// Will reach here if EOF will give Null Pointer Exception, EOF Exception