-2

My code for reading an input stream of numbers and sorts them into an array, returns error:

unreported exception IOException; must be caught or declared to be thrown

on String lines = br.readLine();

 public int[] inputArr()  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String lines = br.readLine();
    String[] strs = lines.trim().split("\\s+");
    int [] a = new int [strs.length];
    for (int i = 0; i < strs.length; i++) {
        a[i] = Integer.parseInt(strs[i]);
    }
    return a ;
}

Any help please on this?

  • Either you add a `throws IOException` to your method signature or you `try {} catch (IOException e) {}` it... You have those two options to *report* possible `Exception`s. – deHaar Nov 07 '18 at 20:34
  • Please [Google your error message](https://www.google.com/search?q=unreported+exception+ioexception+must+be+caught+or+declared+to+be+thrown) (please click on link) before asking as most questions have been asked before, and in this case ***many*** times before. – Hovercraft Full Of Eels Nov 07 '18 at 20:37

1 Answers1

0

BufferedReader.readLine() throws an IOException:

public String readLine() throws IOException {

So any call to it must handle it by either:

  • Surrounding with try/catch block
  • Declaring that your method also throws that exception.
Mike
  • 4,722
  • 1
  • 27
  • 40