java - "String cannot be converted to int" -


this question has answer here:

i have textfile temperatures 12 months. when try find average temperature, error "string cannot converted int" line

temp[counter] = sc.nextline();

someone sees 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 = 0; < temp.length; i++) {     sum += temp[i]; }  double snitt = (sum / temp.length); system.out.println("the average temperature " + snitt); 

you need convert 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 = 0; < temp.length; i++) {           sum += temp[i];      }      double snitt = (sum / temp.length);         system.out.println("the average temperature " + snitt);   } } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -