>扫描每个角色
>检查字符是否在大写字母范围内.
您不需要CapitalCount类来扫描文件.
以下是我的工作解决方案:
public static void main(String[] args) {
Scanner sc = null;
try {
sc = new Scanner(new File("test.txt"));
int count = 0;
while(sc.hasNext()){
String line = sc.nextLine();
for(int i = 0 ; i < line.length(); i++){
if(line.charAt(i) >= 'A' && line.charAt(i) <= 'Z'){
count ++;
}
}
}
System.out.println("The number of capital letters are : "+count);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
finally{
sc.close();
}
}