There is text file with information held on it, with average rainfall per month.

1914,50.9,87,115.8,32.3,47.1,56.6,97.1,63.9,48.1,62.4,110.3,190.8


1914 being the year and the average rainfall follow it.
The year goes up to 2008.
I created a enumeration which has Jan-Dec (months)
And I need to create a class which will store the average rainfall for just one year (array) by using a getter and average rainfall for a specific month.
And another class which is used to store the rainfall data for each available year
(using an array of RainfallYear objects). As well as a method which will return an array of the available years, this class also provides methods to return the mean rainfall for a given year, as well as a method which will calculate the mean rainfall for just a particular month over all available years.

So any pointers is helpful! I've just finished the enumeration

Recommended Answers

All 6 Replies

perhaps you could use arraylists instead of arrays to provide some flexibility.. other than that im not entirely sure how to improve your solution till i can see some code..

But how do I input the data, from the text file?
The text file will have a list, like the one I have posted in the original post.
And I need to collect the data and divide it by 12 to find the average per year.

Read each line, which is a year, and use String.split(",") in order to split for the specific months.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.util.List;
import java.util.ArrayList;

	

	public class RainfallYear {
	private int year;
	private double rainfallMonths;	
	public static void main( String[] args ) {
	List<String> rainList = new ArrayList<String>();

	BufferedReader br = null;
	try {
	br = new BufferedReader( new FileReader( "rain.txt" ) );

	String rain;

	while( ( rain = br.readLine() ) != null )
	rainList.add( rain );
	} catch( IOException e ) {
	e.printStackTrace();
	} finally {
	try {
	br.close();
	} catch( IOException ex ) {
	ex.printStackTrace();
	}
	}

	String[] rain = new String[ rainList.size() ];

	
	rainList.toArray( rain );
	for( int i = 0; i < rain.length; i++ )
	System.out.println( rain[ i ] );
	}
	public int getYear() {
		return year;
	}
	public double getRainfallMonths() {
		return rainfallMonths;
	}
	}

So I can read and display the text file now, with the data inside.
How the heck do I perform manipulation on the data ?!?!
Like calculate average rainfall per year etc

I see that you have full line for each array cell. You can separate your data using the String.split(",") method. After you have split the data, you can use the parseInt method to convert the string to an integer.
After that the answer is math - the average for a year is adding up all the data and dividing by the number of months.

Ok, I've managed this so far:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Rainfall {
	private RainfallYear [] rainfallYear;

	public int [] getYears ()
	{
		int [] YearOfArray = new int [95];
		for ( int n = 0 ; n < 95; n++ )
		{
			YearOfArray [n] = rainfallYear[n].getYear();
		}
		return YearOfArray;
	}
	public double getMeanRainfallYear(int year) 	{
		double meanrainfall = 0;
		for (RainfallYear RainfallAverage : rainfallYear  ){
			meanrainfall += RainfallAverage.calculateMeanRainfall() ;	
			}
			
		meanrainfall /= 95;
			return meanrainfall;
			}
	
	public static void calculateMeanRainfallMonth ()
	{
		
	}
	 
	  public static void main(String[] a) {

	    FileReader in;
	    try {
	      in = new FileReader (new File("rain.txt"));
	      BufferedReader br = new BufferedReader (in);
	      String line; 
	      int count = 0;
	      while ((line = br.readLine())!= null) { 
	        count++; 
	        System.out.println("number of lines is: "+ count);
	        System.out.println(line);       
	      }
	      if (br != null) {
	        br.close(); 
	      }
	    }  
	    catch (FileNotFoundException e) {
	      System.out.println("I can't find such a file");
	      e.printStackTrace(); 
	    } 
	    catch (IOException e) {
	      System.out.println("can't read the information");
	      e.printStackTrace();
	    }

	  }
	  }

In my 'calculateMeanRainfallMonth' method, it is blank because I am stuck!
For this, I need to add up all the average rainfalls, for a given year and then output the mean for this.
EG/
1914, 50.9, 87, 115.8, 32.3, 47.1, 56.6, 97.1, 63.9, 48.1, 62.4, 110.3, 190.8
year - 1914, and the comma separted the year from month (jan-dec).
Any help will be appreciated!

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.