Java未报告的异常读取输入流

时间:2018-11-07 20:33:00

标签: java arrays input compiler-errors

我的代码,用于读取数字输入流并将其排序为数组,然后返回 错误:

未报告的异常IOException;必须被抓住或宣布要扔掉

在字符串行= 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 ;
}

对此有任何帮助吗?

1 个答案:

答案 0 :(得分:0)

BufferedReader.readLine()抛出IOException

public String readLine() throws IOException {

因此,对其的任何调用都必须通过以下任一方式对其进行处理:

  • 用try / catch块包围
  • 声明您的方法也将引发该异常。