【发布时间】:2020-03-12 05:21:14
【问题描述】:
我正在尝试计算文件中的所有整数,但由于.toCharArray(); 和.length(),我不断收到错误消息;他们都是“找不到符号 - 方法 toCharArray()”?
public static void main(String[] args){
try{
FileReader f = new FileReader(("numbers.txt")); //replace this to exact location where you store numbers.txt in computer
Scanner input = new Scanner(f);
char[] count = input.toCharArray();
int num = 0;
for (int i =0; i<input.length(); i++){
if(Character.isDigit(count[i])){
num++;
}
System.out.println("There are "+num+" numbers in the file.");
}
} catch(Exception s){
System.out.println(s.getMessage());
}
}
numbers.txt:
8 96 54 25
104 19
112 86 73
16 30 112 57
2 26 64 83
65
36 1
25
18 111
56 104 8 36 87
我的预期输出如下:
> There are 28 numbers in the file.
【问题讨论】:
-
Scanner input- 现在去阅读Scanner的javadocs