java - "String cannot be converted to int" -
this question has answer here:
- how convert string int in java? 31 answers
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
Post a Comment