BufferedReader providing nonzero exit code

Problem Simple programming question involves reading the number N, T times from the console and performing simple calculations on it.

Limitations:

1 ≀ T ≀ 1000

2 ≀ N ≀ 100000000

Since BufferedReader is usually faster than a scanner, I used it, but the program exited Non-Zero Exit code, while using Scanner solved the problem.

Since both work fine on my computer, I suspect this is a memory issue.

Questions:

  • I believe BufferedReader is faster than a scanner?
  • Does BufferedReader use more memory? If so, is that the cause of the error?

The code:

Using BufferedReader causes an error

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); for (int i=0; i<T; i++) { int N = Integer.parseInt(br.readLine()); int res = (N/2)+1; System.out.println(res); } br.close(); } } 

Code using a scanner that returned the correct output:

 import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); int T = Integer.parseInt(sc.nextLine()); for (int i=0; i<T; i++) { int N = Integer.parseInt(sc.nextLine()); int res = (N/2)+1; System.out.println(res); } sc.close(); } } 
+7
java java.util.scanner bufferedreader
source share
3 answers
  • As of JDK 7, BufferedReader uses a larger buffer than the scanner (I think 8192c versus 1024c), so yes, it uses more memory and can speed up execution on large inputs.
  • This may be the source of your problem (or it may be that the person who wrote the tests for this problem has something wrong), since I myself have tested your BufferedReader code and do not see any problems with it.
+3
source share

I believe BufferedReader is faster than a scanner?

Not in this case, since the speed of your program is limited by how quickly you can enter. Compared to this, any difference between Scanner and BufferedReader is small.

Does BufferedReader use more memory?

Not specified.

If so, is that the cause of the error?

This is the cause of what error? Since you did not post the error you are getting, this question is incontrovertible. However, I see no reason to believe that you have a memory problem.

+1
source share

I had the same error as yours (bug on BufferedReader ). This is because you forgot to put the BufferedReader in the catch try block. I also had throws IOException after the main funtion, but this is not enough. So this is not a memory problem.

The correct code might be something like this:

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = null; try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); for (int i=0; i<T; i++) { int N = Integer.parseInt(br.readLine()); int res = (N/2)+1; System.out.println(res); }catch(Exception e) { e.printStackTrace(); }finally{ try{ br.close(); }catch ( Exception e){ e.printStackTrace(); } } // end of try catch } } 

Good luck

-one
source share

All Articles