-1

So i wanna print this code.. it says:

non - static method rain cannot be accessed from a static context.

the question is to print the names of all the months and their rainfall in parallel columns and their total average and the month with highest and lowest rainfall.
Please help me out.

public class rainfall 
{

    String[] rain =  { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
    double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};


    public double Total( double total )
    {
        total = 0;

        for( int i = 0 ; i < values.length ; i ++ )
        {
            total += values[i];
        }

        return total;
    }

    public double Average( double average)
    {
        double total = 0;
        Total (total);
        for ( int j = 0; j < values.length; j ++ )
        {
            average = total/values.length ;
        }

        return average;
    }

    public String mostRain (double high,String highest )
    {
        high = values[0];

        for(int i = 1 ; i < rain.length ; i ++ )
        {
            if ( values[i]>high )
            {
                highest = rain[i];
            }
            return highest;

        }
    }

    public String leastRain ( String least, double low )
    {
        least = "";
        low= values[0];

        for(int i = 1; i < rain.length ; i++ )
        {
            if ( values[i]<low )
            {
                least = rain[i];
            }

            return least;
        }
    }

    public static void main(String[]Args)
    {
        String highest = "" ;
        String least = "";
        double total = 0 ;
        double average = 0 ;
        double high = 0;
        double low = 0;



        System.out.println(" AUSTIN Tx RAINFALL 2009 " );

        for( int i = 0 ;  i < rain.length ; i ++ )
        {
            System.out.println(rain[i]+"\t"+values[i]);
        }

        System.out.println(Total(total));
        System.out.println(Average(average));
        System.out.println(mostRain(highest,high));
        System.out.println(leastRain(least,low));

    }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
user3339936
  • 31
  • 1
  • 8
  • 1
    You may want to check out the official tutorial on [Class Methods and Fields](http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html). – Jason C Feb 23 '14 at 23:09

6 Answers6

0

Instance variables cannot be accessed in static methods such as main. You must make all of the fields and methods static.

static String[] rain =  { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
static double[] values;
public static String mostRain(...
public static String leastRain(...
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

Your problem is that your main method is static but the other methods and variables are not.

You either need to create an instance of rainfall and call the methods in that instance or make all the methods and variables static too.

Tim B
  • 40,716
  • 16
  • 83
  • 128
0

Change this

String[] rain =  { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};

to this

static String[] rain =  { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
static double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};

or

put the (non-static) variables into the main function itself, and then pass them into the functions as needed. Such as

public static final void main(String[] ignored)  {
   String[] rain =  { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
   double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};

   Total(values);

All functions called by main, as stated by others, either need to be static as well, or alternatively called via a new instance.

(And please, change function names to lowercase.)

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0

You need to add "static" to all your methods, as well as to the variables you defined at the beginning (the arrays "rain" and "values").

You should always consider setting your methods as static whenever you're not using properties defined in the class you're working with (e.g. a method to calculate the average, usually depends only on the values you get as argument).

Bruno Fondevila
  • 159
  • 1
  • 2
0

You are calling a method in a static context:

System.out.println(Average(...));

That means that you are using the method without using it within an object context, i.e. object.Average(...).

You must either

  • Declare the method to be static:

    public static double Average(...) {

  • Or use the method on an object:

    YourObject obj = new YourObject(); obj.Average(...);

PS: Methods should start with a lowercase character.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
0

Since those methods are non static, they can only be accessed through an object of that class. When you run static methods, they are not run through an instantiated object so they don't have access to the non static methods.

So what you would do is create a Rainfall object in main and call the methods on that object. This is how is is usually done. Generally, the main() method doesn't do much work itself but creates an object and calls the methods on that object to do the actual work.

So you would do something like:

public class Rainfall {
    //your methods from above

    public static void main(String[]Args) {
        String highest = "" ;
        String least = "";
        double total = 0 ;
        double average = 0 ;
        double high = 0;
        double low = 0;

        Rainfall rain = new Rainfall();
        System.out.println(" AUSTIN Tx RAINFALL 2009 " );

        for( int i = 0 ;  i < rain.length ; i ++ ) {
            System.out.println(rain[i]+"\t"+values[i]);
        }

        System.out.println(rain.Total(total));
        System.out.println(rain.Average(average));
        System.out.println(rain.mostRain(highest,high));
        System.out.println(rain.leastRain(least,low));

    }

}
Paul J Abernathy
  • 993
  • 2
  • 12
  • 25