2014-09-20 119 views
-1

有在我的代码错误这是给我一个警告“在”永远不会关闭

警告:资源泄漏:“在”永远不会关闭

我以前从来没有过这样的问题,其实我尝试了我编写的旧程序,它仍然给我提出同样的问题。我该如何解决 ?

import java.util.Scanner ; 


public class CountVowels 
{ 
    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Enter a word: "); 
     String word = in.next(); 

     int vowelCount = 0; 
     for (int i = 0; i < word.length(); i++) 
     { 
     char currentCharacter = word.charAt(i); 
     if (currentCharacter == 'a' || currentCharacter == 'A' 
      || currentCharacter == 'e' || currentCharacter == 'E' 
      || currentCharacter == 'i' || currentCharacter == 'I' 
      || currentCharacter == 'o' || currentCharacter == 'O' 
      || currentCharacter == 'u' || currentCharacter == 'U') 
     { 
      vowelCount++; 
     } 
     } 
     System.out.println(vowelCount + " vowel(s)."); 



    } 
} 
+0

关闭了吗?或者,更好的是,使用'try'-with-resources自动关闭它。 – chrylis 2014-09-20 22:53:26

+0

警告与错误不同。 – 2014-09-20 22:53:43

+0

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close() – xlm 2014-09-20 22:54:05

回答

2

你只需要在你的功能与年底前关闭您的扫描仪:

in.close(); 

像这样:

public static void main(String[] args) 
{ 
    Scanner in = new Scanner(System.in); 
    // Do some stuff using the scanner 
    in.close(); 
} 

而且也看看here

+0

谢谢。它现在有效。 – user3251123 2014-09-20 22:56:41

+2

+1,yup :)。更好的办法是让他使用'try/catch'语句,还可以添加'finally'来关闭扫描器 – nem035 2014-09-20 22:56:51