I am trying to write a method that will return the month with the lowest rainfall amount. The problem is that I have to initialize my variable but then my method always returns a "0". How can I fix this problem?

Here is my code:

public int getLowestRainFallMonth()
    {
        double lowest = rainAmount[0];
        int lowMonth = 0;
        for (int index = 1; index < ARRAY_SIZE; index++)
        {
            if (rainAmount[index] < lowest)
            {
                lowest = rainAmount[index];
                lowMonth = index;
            }
        }
        return lowMonth;

OK. I think I figured it out. Not sure if it's the best way to do it but at least it works.

public int getLowestRainFallMonth()
    {
    double lowest = rainAmount[0];
    int lowMonth = (int) rainAmount[0];
    for (int index = 1; index < ARRAY_SIZE; index++)
    {
          if (rainAmount[index] < lowest)
        {
        lowest = rainAmount[index];
        lowMonth = index;
        }
    }
    return lowMonth;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.