Resource Leak Error

前端 未结 4 2027
借酒劲吻你
借酒劲吻你 2020-12-21 13:05

Evening all. I am a complete beginner to programming with Java, and I am learning about \"Scanner\", but when I type this basic code into Eclipse, I get a message saying \"R

4条回答
  •  無奈伤痛
    2020-12-21 13:44

    Because you are nowhere closing your Scanner. This is problem (classes that deal with I/O should be closed after you're done). You need to close it:

    scanner.close();
    

    So a whole code can looks like:

    Scanner scanner = new Scanner(System.in);
    try {
       // work
    }
    finally {
       if (scanner != null) {
          scanner.close();
       }
    }
    

提交回复
热议问题