-1

I am using netbeans and keep getting the following errors:

class interface or enum expected java

class RainFall is public, should be declared in a file named RainFall.java

This is the code:

package rainfalldemo;

/**
 *
 * @author 
 */

    import java.util.Scanner;
    import java.text.DecimalFormat;

    public class RainFallDemo {

    /**
     * begin main method
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // declare variables
        final int MAX_MONTHS = 12;
        double totalRainfall;
        double averageRainfall;
        int monthOfMostRain;
        int monthOfLeastRain;

        //create an array of rainfall
        double[] rainfallInYear = new double[MAX_MONTHS];

        //create array of months in a yeat
        String months[] = {"January", "February", "March", "April", "May",   
       "June", "July", "August", "September", "October", "November", 
       "December"};

        //call getRainfallValues method
        getRainfallValues(rainfallInYear, months);

        // creaet rainfall class object
        RainFall rainInYear = new RainFall(rainfallInYear);

        // create an object for DecimalFormat class
        DecimalFormat decimals = new DecimalFormat("0.00");

        //display total rainfall in the year
        totalRainfall = rainInYear.getTotalRainfall();
        System.out.println("The total rainfall in the year is: " + 
        totalRainfall);

        //display average monthly rainfall
        averageRainfall = rainInYear.getAverageRainfall();
        System.out.println("The average monthly rainfall is: " + 
        decimals.format(averageRainfall));

        //display the month with most rain
        monthOfMostRain = rainInYear.getMonthOfMostRainfall();
        System.out.println("The month with the most rain: " + 
        rainfallInYear[monthOfMostRain] + " in " + months[monthOfMostRain]);

        //display month with least rain
        monthOfLeastRain = rainInYear.getMonthOfLeastRainfall();
        System.out.println("The month with the least rain: " + 
        rainfallInYear[monthOfLeastRain] + " in " + months[monthOfLeastRain]);

        }//end of main method

        //getRainfallValues method implementation
        public static void getRainfallValues(double[]rainfallInEachMonth, 
        String[]    monthNames)
        {
        //create an object for Scanner class
        Scanner input = new Scanner(System.in);

        // get rain for every month
        for(int i = 0; i < rainfallInEachMonth.length; i++)
        {
            //prompt user for rainfall in a month
            System.out.print("enter the total rainfall for the month " + 
            monthNames[i] + ": ");
            rainfallInEachMonth[i] = input.nextDouble();
            /* verify whether the rain in the month is negative */
                    while(rainfallInEachMonth[i] < 0)
                    {
                        System.out.print("Enter positive value for rainfall: ");
                        rainfallInEachMonth[i] = input.nextDouble();
                    } //end while
        } //end for
        System.out.println();
    } //end of getRainfallValues method

    }//end of RainfallDemo class

RainFall.java

    //RainFall class

    public class RainFall
    {
    // variable declaration
    private double[] rainInYear;

    public RainFall(double[] rainfallInEachMonth)
    {
        rainInYear = new double[rainfallInEachMonth.length];

        //store rainfall
        for(int  i = 0; i < rainfallInEachMonth.length; i++)
            rainInYear[i] = rainfallInEachMonth[i];
    } // end of parameterized constructor

      //get TotalRainfall method implementation
    public double getTotalRainfall()
    {
        //local variable 
        double totalRainfall = 0.0;
        //calculate the total rainfall in the year
        for(int i= 0; i < rainInYear.length; i++)
            totalRainfall += rainInYear[i];
        return totalRainfall;
    } // end getTotalRainfall method

    //getAverageRainFall method implementation
    public double getAverageRainfall()
    {
        //local variable
        double averageRainfall = 0.0;

        //calculate the average rainfall in a year
        averageRainfall = getTotalRainfall() / rainInYear.length;
        //return averageRainfall
        return averageRainfall;
    } // end of getTotalRainfall method

    //getMonthOfMostRainfall method implementation
    public int getMonthOfMostRainfall()
    {
        // local variable
        double most;
        int monthNumber;
        //initialize variables

        most = rainInYear[0];
        monthNumber = 0;
        //loop repeats to find the month of most rainfall
        for(int i = 1; i < rainInYear.length; i++)
        {
            // find the most rainfall
            if(rainInYear[i] > most)
            {
                most = rainInYear[i];
                monthNumber = i;
            }//end if 
        }  //end for loop
     //return month of most rainfall
        return monthNumber;
    } //end of getMonthOfMostRainfall method

    //getMonthOfLeastRainfall method implementation
    public int getMonthOfLeastRainfall()
    {
        //local variable
        double least;
        int monthNumber;
        //initialize the variables

        least = rainInYear[0];
        monthNumber = 0;
        //loop repeats to find the month of least rainfall
        for(int i = 1; i < rainInYear.length; i++)
        {
            //find least rainfall month
            if(rainInYear[i] < least)
            {
                least = rainInYear[i];
                monthNumber = i;
            }// end if
        } // end for loop
        // return the month of least rainfall
        return monthNumber;
    }// end of getMonthOfLeastRainfall method

} // end of RainFall class
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Wil Cuz
  • 1
  • 1
  • Remove the `public` Access modifier with `class RainFall` – Madushan Perera Dec 01 '15 at 19:30
  • 2
    It's unclear whether this code sample is all contained in one file... The OP has `RainFall.java` in the middle, which implies he's quoting from a separate file. The OP also has a bizarre casing issue (`RainFAll is public`...) in his quoted error message, but that may be due to user-error when quoting into his post. – Kirk Woll Dec 01 '15 at 19:35
  • Now it appears that `RainFall` is in its own file. But that makes the error message nonsensical. @Wil, would you please clarify? Is `RainFall` in its own file, RainFall.java? Or in the same file as `RainFallDemo`? – Erick G. Hagstrom Dec 01 '15 at 20:29
  • It should all be in the same file. – Wil Cuz Dec 02 '15 at 03:24
  • Madushan, you're a genius. thank you very much. And thanks to everyone for your help I greatly appreciate your responses. I look forward to helping others in the future just as you have helped me. Removing the public modifier with class RainFall worked. Thank you. – Wil Cuz Dec 02 '15 at 04:19

1 Answers1

1

You can not have more than one public class in a single file.Take it out and put it in a file as suggested to you in error and you will be good.

From Documentation

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler to find a named class within a package. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.

See Also

Community
  • 1
  • 1
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35