8 Replies - 864 Views - Last Post: 02 December 2015 - 03:29 PM Rate Topic: -----

#1 URoutofURelement   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-December 15

SO Close, Rainfall Array problem. Can't get Lowest Rainfall

Posted 02 December 2015 - 01:21 PM

Please be gentle, I know that this is not the most efficient or visually pleasing way to present this code...but it is working for me so far. EXCEPT I cannot get the "lowmonth" and "least" output to display. When I got the Most portion of the code to give the correct output, I simply used the same code and changed the variables and then the "<" sign. However, this is giving me a "0" and "0.0" for my Least output respectively. What did I do wrong?

No errors pop up using NetBeans

public class Source1 
{

    public static void main(String[] args) 
    {
        int[] month = new int [12];
        double[] rain = new double [12];
        
        Scanner keyboard = new Scanner(System.in);
        
        for (int index = 0; index < rain.length; index++)
        {
                    System.out.print("Enter the rainfall for month "+(index+1)+": ");
                    rain[index] = keyboard.nextDouble();
                    
                while(rain[index]<0)
                {
                System.out.print("Invalid. Enter a positive number for rainfall: ");
                rain[index]=keyboard.nextDouble();     
                }
        }
                TotalRainfall(rain);
                Average(rain);
                Most(rain, month);
                Least(rain, month);
               
    }
    
    public static void TotalRainfall(double[] rain) 
    {
        double total = 0;
        for (int index=0; index<rain.length; index++)
        {
            total += rain[index];
        }
        
        System.out.println("\nTotal rainfall for this year is : "+ total);
        
    }
    
    public static void Average(double[] rain) 
    {
        double total = 0; 
        double average;
        for (int index = 0; index<rain.length; index++)
        {
            total += rain [index];
        }
            average = total/12;
        
        System.out.println("Average rainfall for this year is : "+ average);
    }
    
    public static void Most(double[] rain, int[] month)
    {
        double most;
        int highmonth;
        most = (int) month[0];
        highmonth = (int) month[0];
       
        for (int index=0; index<rain.length; index++)
        {
            if (rain[index]>most)
            {
                most= rain[index];
                highmonth= index + 1;
            }
        }
         
        System.out.println("Month with the most rainfall is " + highmonth +" with a rainfall of "+ most );
        
    }    
        
        
     public static void Least(double[] rain, int[] month)
    {
        double least;
        int lowmonth;
        least = (int) month[0];
        lowmonth = (int) month[0];
       
        for (int index=0; index<rain.length; index++)
        {
            if (rain[index]<least)
            {
                least=rain[index];
                lowmonth= index + 1;
            }
        }
         
        System.out.println("Month with the least rainfall is " + lowmonth +" with a rainfall of "+ least );
        
    }

}


This post has been edited by macosxnerd101: 02 December 2015 - 01:25 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: SO Close, Rainfall Array problem. Can't get Lowest Rainfall

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: SO Close, Rainfall Array problem. Can't get Lowest Rainfall

Posted 02 December 2015 - 01:30 PM

There is a trick when searching for max and min values. Set the initial value of the min or max variable to the first element in the array and then compare the rest of the array against that value.

If the initial value for min is the default of 0, none of the elements in the array will be less than that.

This post has been edited by NormR: 02 December 2015 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: SO Close, Rainfall Array problem. Can't get Lowest Rainfall

Posted 02 December 2015 - 01:38 PM

Or use something like Double.MAX_VALUE
Was This Post Helpful? 0
  • +
  • -

#4 URoutofURelement   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-December 15

Re: SO Close, Rainfall Array problem. Can't get Lowest Rainfall

Posted 02 December 2015 - 02:00 PM

Okay. I think I fixed some of the things you were hinting at Norm.

I changed my code up...everything works PERFECT - Except for when either the highest or lowest month is in month 1 - that is giving me a "highmonth" or "lowmonth" being equal to "0"


public class Source1 
{

    public static void main(String[] args) 
    {
        int[] month = new int [13];
        double[] rain = new double [13];
        
        Scanner keyboard = new Scanner(System.in);
        
        for (int index = 1; index < rain.length; index++)
        {
                    System.out.print("Enter the rainfall for month "+(index)+": ");
                    rain[index] = keyboard.nextDouble();
                    
                while(rain[index]<0)
                {
                System.out.print("Invalid. Enter a positive number for rainfall: ");
                rain[index]=keyboard.nextDouble();     
                }
        }
                TotalRainfall(rain);
                Average(rain);
                Most(rain, month);
                Least(rain, month);
               
    }
    
    public static void TotalRainfall(double[] rain) 
    {
        double total = 0;
        for (int index=1; index<rain.length; index++)
        {
            total += rain[index];
        }
        
        System.out.println("\nTotal rainfall for this year is : "+ total);
        
    }
    
    public static void Average(double[] rain) 
    {
        double total = 0; 
        double average;
        for (int index = 1; index<rain.length; index++)
        {
            total += rain [index];
        }
            average = total/12;
        
        System.out.println("Average rainfall for this year is : "+ average);
    }
    
    public static void Most(double[] rain, int[] month)
    {
        double most;
        int highmonth;
        most = rain[1];
        highmonth = month[1];
       
        for (int index=1; index<rain.length; index++)
        {
            if (rain[index]>most)
            {
                most= rain[index];
                highmonth= index;
            }
        }
         
        System.out.println("Month with the most rainfall is " + highmonth +" with a rainfall of "+ most );
        
    }    
        
        
     public static void Least(double[] rain, int[] month)
    {
        double least;
        int lowmonth;
        least = rain[1];
        lowmonth = month[1];
       
        for (int index=1; index<rain.length; index++)
        {
            if (rain[index]<least)
            {
                least=rain[index];
                lowmonth= index;
            }
        }
         
        System.out.println("Month with the least rainfall is " + lowmonth +" with a rainfall of "+ least );
        
    }

}



Any ideas as to why this is happening? Because I've been trying to change the index and what not and I'm not getting anywhere on this last issue.

Also, thank you to everyone who took the time to read my post and offer any suggestions. I really appreciate your time and effort!
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: SO Close, Rainfall Array problem. Can't get Lowest Rainfall

Posted 02 December 2015 - 02:04 PM

You had it right before. You need to add 1 to the index. Btw one array is largely redundant
Was This Post Helpful? 0
  • +
  • -

#6 URoutofURelement   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-December 15

Re: SO Close, Rainfall Array problem. Can't get Lowest Rainfall

Posted 02 December 2015 - 02:07 PM

View Postg00se, on 02 December 2015 - 02:04 PM, said:

You had it right before. You need to add 1 to the index. Btw one array is largely redundant


But when I add one to the index now, it gives me months "13" and still month "0" when I type in 1-12 in sequential order for the rainfall input
Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: SO Close, Rainfall Array problem. Can't get Lowest Rainfall

Posted 02 December 2015 - 02:07 PM

Note: The index of the first element in an array is 0 not 1.
Was This Post Helpful? 1
  • +
  • -

#8 URoutofURelement   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-December 15

Re: SO Close, Rainfall Array problem. Can't get Lowest Rainfall

Posted 02 December 2015 - 02:53 PM

View PostNormR, on 02 December 2015 - 02:07 PM, said:

Note: The index of the first element in an array is 0 not 1.



Thanks to both of you guys. I got it! Instead of trying to add 1 to the index I just added 1 to the highmonth and lowmonth and now seem to have the correct output to all different variations of months with the high and low rainfall!!!

Thank you again! So helpful!
Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: SO Close, Rainfall Array problem. Can't get Lowest Rainfall

Posted 02 December 2015 - 03:29 PM

No problem. Get rid of the array called 'month' - it's redundant
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1