1

Version 2 of my monthly rainfall program, with edits and suggestions applied. Thanks so much for your help it really made me think. Wonderful thing about programming is how complete strangers can come together to solve problems.

// Rainfall Class Start 

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Arrays;

/**
 *
 * @author Adrian
 */
public class Rainfall{
   DecimalFormat twoDecimals = new DecimalFormat("##0.00");
   private final String[] Months={"Jan","Feb","Mar","Apr","May","Jun",
                           "Jul","Aug","Sep","Oct","Nov","Dec"};;
   private final double[] Rainfall;

   public Rainfall() throws IOException
   {
      Rainfall = new double[12];
   }
   public double getAverageRainFall()
   {
      return getTotalRainFall() / 12 ;
   }
   public void outputToFile()throws IOException
       {
       try (PrintWriter outputFile = new PrintWriter("Months.txt")) {
           Scanner kb=new Scanner(System.in);

           System.out.println("For each month enter a rainfall amount");

           for (int i=0;i<12;i++)
           {
               System.out.println(Months[i]+"?");
               Rainfall[i]=kb.nextDouble();
               outputFile.println(Months[i]+" "+Rainfall[i]);
               System.out.println(Months[i]+" "+Rainfall[i]);
           }
       }
       } 

    /**
     *
     * @return 
     */
    public double getTotalRainFall()
   {
      double total = 0.0;  // Accumulator initialized to 0

      // Accumulate the sum of the rain array elements.
      for (int i = 0; i < Rainfall.length; i++)
         total += Rainfall[i];

      // Return the sum.
      return total;
   }
    public double getLowestMonth()
   {
      int lowest = 0;

      // Find the element with the lowest value.
      for (int i = 1; i < Rainfall.length; i++)
      {
         if (Rainfall[i] < Rainfall[lowest])
            lowest = i;
      }
      // Return the element number.
      return lowest+1;
      }
    public int getHighestMonth()
   {
      int highest = 0;

      // Find the element with the highest value.
      for (int i = 1; i < Rainfall.length; i++)
      {
         if (Rainfall[i] > Rainfall[highest])
            highest = i;
      }
      // Return the element number.
      return highest;
   }
    public void displayMessage(){
          System.out.println("----------------------");
          System.out.println("Monthly Rainfall Totals For 2014"); 
          System.out.println(Arrays.toString(Months));
          System.out.println(Arrays.toString(Rainfall));
          System.out.println("Average Rainfall = "  +getAverageRainFall());
          System.out.println("Total Rainfall   = "  +getTotalRainFall());
          System.out.println("Highest Month    = "  +getHighestMonth());
          System.out.println("Lowest Month     = "  +getLowestMonth());
          System.out.println("----------------------");
}
    public double getRainAt(int e)
   {
      return Rainfall[e];
   }
   }



  /*public void inputFromFile() throws IOException
   {
      File myFile=new File ("Months.txt");
      Scanner inputFile=new Scanner(myFile);

      //loop through the file

      for(int i=0;i<12;i++){
         Months[12] = inputFile.nextLine();
         inputFile.close();
      }
   }*/

// End Class // Start of Main Method

import java.io.*; import java.text.DecimalFormat;

public class Rainfall2{

    public static void main(String[] args) throws IOException
      {
             // The low month

         DecimalFormat twoDecimals = new DecimalFormat("##0.00");
         Rainfall myData=new Rainfall();

         myData.outputToFile();
         myData.displayMessage();

      }
}

Version 2 of my monthly rainfall program, with edits and suggestions applied. Thanks so much for your help it really made me think. Wonderful thing about programming is how complete strangers can come together to solve problems.

4 Answers4

3

The compiler is actually right, You can't call a non stati method from your static main :) , call that getTotalRainFall(); on a instance of Rainfall.

Edit1: Thanks puj, fixed the error.

Edit2: Actually, your code looks a bit messed up, what is that Prices class? Should you have Rainfall there? End end up with something like:

RainFall rf=new RainFall();
rf.inputFromFile();
rf.getTotalRainFall();
rf.outputToFile();

Crystal, can i recommend you to rewrite it from scratch with the suggestions you got from these answers?

uraimo
  • 19,081
  • 8
  • 48
  • 55
2

Also, you will probably want to change

  for(int i=0;i<12;i++)
     Months[i]=inputFile.nextLine();
     inputFile.close();

to

  for(int i=0;i<12;i++)
     Rainfall[i]=inputFile.nextLine();
     inputFile.close();

to get the correct data.

puj
  • 770
  • 5
  • 9
1

The method is part of the object, that why you need instance of the object to call his method, you can do something like this in your main:

Rainfall r = new Rainfall();
r.getTotalRainFall();
Roy Shmuli
  • 4,979
  • 1
  • 24
  • 38
0

Make public double getTotalRainfall() static,

public static double getTotalRainfall(){}

so you can call it from main with double totalRain = Rainfall.getTotalRainfall();

You would have to make private double[] rainFall; also static, private static double[] rainFall;

And you should concider using another name for the array Rainfall and Months according to standard naming in Java, you should use a lower case verb to declare fields. Else it might cause problems.

WonderWorld
  • 956
  • 1
  • 8
  • 18