我收到了资源泄漏:'in'永远不会关闭警告

时间:2016-11-01 19:45:20

标签: java arrays

public class ArrayReversal {
    public static void main(String[] args) {
        double[] numbers = readInputs(5);
        printReversed(numbers);
    }

    public static double[] readInputs(int numberOfInputs) {
        System.out.println("Enter " + numberOfInputs + "numbers: ");
        Scanner in = new Scanner(System.in);
        double[] inputs = new double[numberOfInputs];
        for (int i = 0; i < inputs.length; i++) {
            inputs[i] = in.nextDouble();
        }
        return inputs;
    }

    public static void printReversed(double[] values) {
        for(int i = values.length - 1; i>= 0; i--) {
            System.out.print(values[i] + " ");
        }
    }
    //in.close();
}

我有close()注释,因为它给了我一个语法错误,说明了一个标识符。所以我不知道该把它放在哪里。

1 个答案:

答案 0 :(得分:2)

  

&#39;在&#39;永远不会关闭警告

如此接近它。

 public static double[] readInputs(int numberOfInputs) {
        System.out.println("Enter " + numberOfInputs + "numbers: ");
        Scanner in = new Scanner(System.in);
        double[] inputs = new double[numberOfInputs];
        for (int i = 0; i < inputs.length; i++) {
            inputs[i] = in.nextDouble();
        }
        //be aware: you are globally closing System.in
        in.close();
        return inputs;
    }