I keep getting an InputMismatchException even though all types are correct , what should ido?

62 Views Asked by At
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

2

There are 2 best solutions below

0
On
        // ...
        double sum = 0.0;
        String line;
        while (!(line = sc.nextLine()).isBlank()) { // 1
            String[] splitLine = line.trim().split(" "); // 2
            discription = splitLine[3];
            if (discription.equals("Temperatur")) {
                value = Double.parseDouble(splitLine[4]);
                sum = sum + value;
                if (value >= warmest) {
                    warmest = value;
                    year = Integer.parseInt(splitLine[0]);  //3
                    month = Integer.parseInt(splitLine[1]);
                    day = Integer.parseInt(splitLine[2]);
                }
            }
        }
        sc.close();
        // ...

Remarks:

  • //1 you need to know when to stop reading the input - you can check if the next line is blank for example
  • //2 to avoid the problem mentioned in the comment, I would read the whole line and parse each of the space separated parts to the expected types
  • //3 seems that you want to store the year, month and day of the warmest day, so it should be updated only when you find the higher temperature.
0
On

Your logic for reading each line via your Scanner is incorrect. Looking at just this part of your code that is supposed to read a single line:

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();

When you call sc.nextLine(), you've now read the entire rest of the line and assigned it to discription. So the check that follow will never be true, because even where it should succeed, discription will be Temperatur 16.8. But I think you get off track before you ever get there. When you call sc.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 call nextInt() to read the day value, and you get the description value instead of the day value, which of course, can't be parsed as an integer.