【发布时间】:2017-03-30 14:24:08
【问题描述】:
程序计算特定字符、字母或数字在句子中出现的次数。
但是我不断收到消息:
资源泄漏:'sc' 永远不会关闭
我正在使用 Java 和 Eclipse。我该怎么办?
import java.util.Scanner;
class Number-count {
public static void number - count(String args[]) {
String s;
char ch;
int count = 0;
Scanner SC = new Scanner(System. in );
System.out.println("Enter a sentence");
String str = sc.nextLine();
System.out.println("Enter a character to be searched for occurence");
s = sc.nextLine();
char c = s.charAt(0);
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if (ch == c) {
count++;
}
}
System.out.println("Character " + c + " occur " + count + " times");
}
}
【问题讨论】:
标签: java memory-leaks java.util.scanner