警告:资源泄漏:'键盘'永远不会关闭

时间:2014-10-06 01:04:14

标签: java

import java.util.Scanner;

public class LetterGrade {
  public static void main(String[] args) {
    char grade;
    String input;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter grade awarded");

    input = keyboard.nextLine();

    grade = input.charAt(0);

    switch (grade) {
      case 'A':
        System.out.println("Grade was between 89.5 and 100");
        break;  
      case 'B':
        System.out.println("Grade was between 79.5 and 89.49"); 
        break;  
      case 'C':
        System.out.println("Grade was between 69.5 and 79.49");
        break; 
      case 'D':
        System.out.println("Grade was between 59.5 and 69.49");
        break;
      case 'F':
        System.out.println("Grade was below 59.5");
        break;
      default:
        System.out.println("Invalid grade inputted");
        break;
    }
  }
}
  

[line:9]警告:资源泄漏:'键盘'永远不会关闭

这是我收到的警告。我不认为消息来源实际上已被泄露。 但我不知道怎么把它拿掉所以我可以编译代码

3 个答案:

答案 0 :(得分:4)

忽略该警告是安全的,我永远不会close()包裹全局System.in的扫描程序

答案 1 :(得分:3)

您需要关闭键盘扫描仪。 keyboard.close()

答案 2 :(得分:0)

您打开了键盘:

Scanner keyboard = new Scanner(System.in); 

当不再需要键盘输入时,需要使用keyboard.close()方法关闭它。