"String cannot be converted to int"
This question already has an answer here:
您需要将sc.nextLine转换为int
Scanner sc = new Scanner(new File("temperatur.txt"));
int[] temp = new int [12];
int counter = 0;
while (sc.hasNextLine()) {
String line = sc.nextLine();
temp[counter] = Integer.ParseInt(line);
counter++;
}
int sum = 0;
for(int i = 0; i < temp.length; i++) {
sum += temp[i];
}
double snitt = (sum / temp.length);
System.out.println("The average temperature is " + snitt);
}
}
Scanner::nextLine returns a String. In Java you can't cast a String to an int like you do implicitly.
try
temp[counter] = Integer.parseInt(sc.nextLine());
Your sc.nextLine() returns String
https://www.tutorialspoint.com/java/util/scanner_nextline.htm
链接地址: http://www.djcxy.com/p/20918.html上一篇: 如何将InputStream转换为int
下一篇: “字符串不能转换为int”