Hi all,

I really need your help on this as I am just learning Java. I do have an error message which says "cannot find symbol on line 23.
Many thanks.

import javax.swing.JOptionPane;

public class averageRainfall {

    public static void main(String[] args) {

        //declare variables
        String[] Month = new String[6];
        int[] Rainfall = new int[6];
        

        //for loop to work through the array
        for (int i = 0; i < 6; i++) {
            Month[i] = JOptionPane.showInputDialog(null, "Please enter the name of the month you want to enter the rainfall for");
            Rainfall[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the amount of rainfall in centimetres for each month"));
        }

        averageCalculation(Month, Rainfall);


        //print average rainfall per month for the six month period
        JOptionPane.showMessageDialog(null, "The average of rainfall is " + average + " centimetres per month");

        System.exit(0);

    }

    public static int averageCalculation(String Month[], int Rainfall[]) {
        int total = 0;
        //calculate average
        for (int i = 0; i < Rainfall.length; i++) {
            total += Rainfall[i];
        }

        int average = total / 6;
        
        //return result
        return average;


    }
}

I do have an error message which says "cannot find symbol on line 23.

It would help if you posted the full text of the error message. Your post has left off important data.
Please copy full text of error message and paste it here. Here is a sample:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^
public static int averageCalculation(String ...

This method returns a value. Your code that calls it does not save that value in a variable.

Member Avatar for hfx642

average has been defined "outside" of main.
Therefore, main can not find it.

Try doing...
1. Remove line 19.
2. Change line 23 to

JOptionPane.showMessageDialog(null, "The average of rainfall is " + averageCalculation(Month, Rainfall) + " centimetres per month");

Thank you very much hfx642! It is now working. But could you tell me why I could not call the method as I did and if you have any documentation of this so it would help me to learn Java a bit better? Many thanks.

why I could not call the method as I did

This method returns a value. Your code that calls it does not save that value in a variable.

averageCalculation(Month, Rainfall);  // returned value not used???

vs

int average = averageCalculation(Month, Rainfall); // save value in variable

Thanks a lot for your support. This forum is really great!

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.