Idiot
Idiot

Reputation: 55

How would I fix "Possible Lossy Conversion from Double to Int" for this class?

The goal of this class is to be able to take input for rainfall totals from the user. After taking the input, the program will display rainfall totals for the year, average monthly rainfall, month with the most rain and month with the least rain. Here is my code:

import java.util.Scanner;

public class Rainfall {
    private static final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        final int MONTHSINYEAR = 12;
        double[] rainfall = new double[MONTHSINYEAR];

        System.out.println("Enter the monthly rain fall amount: ");
        int i = 0;

        for (i = 0; i < MONTHSINYEAR; i++){
            do {
                System.out.println("Enter rainfall for month " + (i + 1) + ": ");
                rainfall[i] = input.nextDouble();
            }
            while (rainfall[i] < 0);
        }

        System.out.println("The total rainfall for the year is: " + totalRainfall(rainfall));
        System.out.println("The average monthly rainfall is: " + averageRainfall(rainfall));
        System.out.println("The month with the most rain is: " + months[maxRainfall(rainfall)]);
        System.out.println("The month with the least rain is: " + months[minRainfall(rainfall)]);
    }

    public static double totalRainfall(double[] arr){
        double total = 0;
        for (int i = 0; i < arr.length; i++){
            total += arr[i];
        }

        return total;
    }

    public static double averageRainfall(double[] arr){
        double average = 0;
        average = totalRainfall(arr) / arr.length;
        return average;
    }


    public static double maxRainfall(double[] arr){
        double max = arr[0];
        double maximum = 0;

        for (int i = 0; i < arr.length; i++){
            if (arr[i] > max){
                max = arr[i];
                maximum = i;
            }
        }
        return maximum;
    }

    public static double minRainfall(double[] arr){
        double min = arr[0];
        double minimum = 0;

        for(int i = 0; i < arr.length; i++){
            if(arr[i] < min){
                min = arr[i];
                minimum = i;
            }
        }
        return minimum;
    }
}

When compiling, the following lines obtain an error that says "Possible lossy conversion from double to int":

        System.out.println("The month with the most rain is: " + months[maxRainfall(rainfall)]);
        System.out.println("The month with the least rain is: " + months[minRainfall(rainfall)]);

I am not sure of how to proceed. What should be changed so those two lines do not have errors?

Upvotes: 0

Views: 37

Answers (2)

Scary Wombat
Scary Wombat

Reputation: 44813

Your method is returning a double, change it to an int (and the method signature) so that it is returning the index of the maximum value

public static int maxRainfall(double[] arr){
    double max = arr[0];
    int maximum = 0;

    for (int i = 0; i < arr.length; i++){
        if (arr[i] > max){
            max = arr[i];
            maximum = i;
        }
    }
    return maximum;
}

Upvotes: 2

rzwitserloot
rzwitserloot

Reputation: 102814

the index to an array lookup expression (the idx in expressionOfTypeArray[idx]) must be of type int, but you're providing a double. What the error is trying to tell you is that perhaps that double could be, say, 5.5, or 2^100; both of these numbers have no equivalent int value. Maybe you analyse the code and conclude that this can't happen, but the compiler doesn't know this, so out of defensive principles it won't silently try to smash the square peg that is a double into the round hole that is an int just because that's the only thing that works here.

You could simply.. tell the compiler that, yeah, smash it in there: months[(int) maxRainfall(rainfall)]); - this will round the number by lopping off any fractional part, so, 5.5 becomes 5, silently.

But, I'd fix your methods. The maxRainfall method returns the index of the month when the max rainfall occurred. There is absolutely no reason whatsoever why that should be a double; there is, after all, no "5.5th month". Just make that public static int minRainfall(... and it'll work fine.

Upvotes: 0

Related Questions