有在我的代码错误这是给我一个警告“在”永远不会关闭
警告:资源泄漏:“在”永远不会关闭
我以前从来没有过这样的问题,其实我尝试了我编写的旧程序,它仍然给我提出同样的问题。我该如何解决 ?
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).");
}
}
关闭了吗?或者,更好的是,使用'try'-with-resources自动关闭它。 – chrylis 2014-09-20 22:53:26
警告与错误不同。 – 2014-09-20 22:53:43
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close() – xlm 2014-09-20 22:54:05