Results 1 to 3 of 3
  1. #1
    Kazyu is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Code Error+ problems with inputting array lists into methods?

    Heres the code I'm having trouble with
    The problem I'm experiencing is "error: non-static method totalRain(double[]) cannot be referenced from a static context". Which Im guessing
    is caused because of improper method calling or inputting rainfall. Is there any way to have my rainfall array be used by the methods?

    Java Code:
    /**
     * @(#)Ch7_Q1.java
     *
     * Ch7_Q1 application
     *
     * @author Peter Wu CSC 210
     * @version 1.00 2013/2/14
     *Write a RainFall class that stores the total rainfall for each of 12 months into an array of
    doubles. The program should have methods that return the follow ing;
     the total rainfall for the year
     the average monthly rainfall
     the month with the DOM rain
     the month with the least rain
     */
    
     import java.util.Scanner;
    public class Ch7_Q1 {
        
        public static void main(String[] args) {
        	Scanner keyboard=new Scanner(System.in);
        	final int month=12;
        	double[] rainfall= new double[month];
        	for (int x=0;x<12;x++)
        		{
        		System.out.println("What was the rainfall for month "+(x+1)+":");
        		rainfall[x]=keyboard.nextDouble();
        		if(rainfall[x]<0)
        		{System.out.println("Please input a valid rainfall number:");
        			rainfall[x]=keyboard.nextDouble();
        		}
        		}
        		//These are the parts Im having trouble with
        		System.out.println("The total rainfall is "+totalRain(rainfall));
        		System.out.println("The avg rainfall is "+ avgRain(rainfall));
        		System.out.println("The month with the lowest rainfall is month "+minRain(rainfall));
        		System.out.println("The month with the most rainfall is month "+maxRain(rainfall));
        }
        //Methods
       public double totalRain(double[] i)
       {double sum=0;
       	for (int x=0;x<12;x++){
       	sum+=i[x];
       }
       return sum;
       }
       public double avgRain(double total)
       {double avg=total/12;
       return avg;
       }
       public double maxRain(double[] i)
       {double max=i[1];
       int month=0;
       for (int x=0;x<12;x++)
       {if(max<i[x]){
       	max=i[x];
       	month=x;}
       }
       return month;
       }
       public double minRain(double[] i)
       {double min=i[1];
       int month=0;
       for (int x=0;x<12;x++){
       	if(min>i[x]){
       		min=i[x];
       		month=x;
       	}
       }
       return min;
       }
    }

  2. #2
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Code Error+ problems with inputting array lists into methods?

    Hi Kazyu,

    Yes, you code have problem with method. Because main method is static that is not call non-static method. you'll change non-static methods to static methods..like below..


    Java Code:
      //Methods
           public static double totalRain(double[] i)
           {double sum=0;
            for (int x=0;x<12;x++){
            sum+=i[x];
           }
           return sum;
           }
           public static double avgRain(double total)
           {double avg=total/12;
           return avg;
           }
           public static double maxRain(double[] i)
           {double max=i[1];
           int month=0;
           for (int x=0;x<12;x++)
           {if(max<i[x]){
            max=i[x];
            month=x;}
           }
           return month;
           }
           public static double minRain(double[] i)
           {double min=i[1];
           int month=0;
           for (int x=0;x<12;x++){
            if(min>i[x]){
                min=i[x];
                month=x;
            }
           }
           return min;
           }
    Now your code is run successfully... Then you have made another mistake on your code for calculate the average method avgRain(rainfall). you passed array as a parameter. You created a method only used double variable like double avgRain(double total)..So will change the code like below


    Java Code:
    double sum=totalRain(rainfall);
     System.out.println("The avg rainfall is "+ avgRain(sum));
    Last edited by tamilarasi; 02-26-2013 at 10:04 AM.
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  3. #3
    Kazyu is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Code Error+ problems with inputting array lists into methods?

    Thank you so much for your help and wow I didnt notice the mistakes until you pointed them out.

Similar Threads

  1. help inputting values into an array
    By pds8475 in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 06:45 PM
  2. Array Lists help!!
    By lilika in forum New To Java
    Replies: 12
    Last Post: 01-04-2011, 03:05 PM
  3. Problems With Array/Methods
    By blueduiker in forum New To Java
    Replies: 4
    Last Post: 01-19-2010, 02:49 AM
  4. Eclipse auto-complete lists duplicate methods
    By Red Hunt in forum Eclipse
    Replies: 1
    Last Post: 12-10-2009, 11:53 AM
  5. please i need the code of comparing these two array lists.
    By raj reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 04-18-2008, 08:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •