import java.util.Scanner;
public class Messdaten {
public static double temperaturInDurchschnitt(Scanner sc){
int year= 0, month= 0 , day= 0;
String discription = "";
double value= 0.0;
double warmest= -273.15;
int i = 0;
double sum = 0.0;
while (sc.hasNext()) {
year= sc.nextInt();
month= sc.nextInt();
day= sc.nextInt();
discription = sc.nextLine();
if (discription.equals("Temperatur")){
value= sc.nextDouble();
sum = sum + value;
if (value>= warmest){
warmest = value;
}
}
value = sc.nextDouble();
} sc.close();
System.out.println("highest Temperatur " + "(" + warmest+ ")" + "at" + day+ month+ year);
return sum/i;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
temperaturInDurchschnitt(sc);
}
}
i am trying to gather the values of temperatur only from a given text:
2018 10 16 Luftdruck 1014.7
2018 10 17 Niederschlag 1.3
2018 10 15 Temperatur 18.2
2018 10 16 Niederschlag 0.0
and return the highest temp at which day .
2018 10 17 Temperatur 16.8
Your logic for reading each line via your
Scanneris incorrect. Looking at just this part of your code that is supposed to read a single line:When you call
sc.nextLine(), you've now read the entire rest of the line and assigned it todiscription. So the check that follow will never betrue, because even where it should succeed,discriptionwill beTemperatur 16.8. But I think you get off track before you ever get there. When you callsc.nextDouble(), you won't get the value at the end of a line, because you've already read the whole line. Instead, you'll get the year value from the next line. And now you're out of sync with your incoming data. I expect that the error comes when you callnextInt()to read thedayvalue, and you get the description value instead of the day value, which of course, can't be parsed as an integer.