This question already has an answer here:
- How do I convert a String to an int in Java? 43 answers
I have a textfile with temperatures from all 12 months. But when I try to find the average temperature, I get the error "String cannot be converted to int" to the line
temp[counter] = sc.nextLine();
Can someone say what's wrong?
Scanner sc = new Scanner(new File("temperatur.txt"));
int[] temp = new int [12];
int counter = 0;
while (sc.hasNextLine()) {
temp[counter] = sc.nextLine();
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);