Results 1 to 4 of 4
  1. #1
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    16
    Rep Power
    0

    Default Please help been stuck for days now!!

    Hello all,

    i am fairly new to java but have made a few projects so i do know most aspects that would allow me to complete the task its just i have been stuck at the same place now for 2 days !!

    The project is to create a java program that can read specific data within a csv file to work out the averages etc. The csv file is a database of different weather stats . an example of the first 12 months of the csv file is below..

    [CSV FILE]

    year ,month,average rainfall etc
    1946 1 5.7 0.6 16 108.1 31.5
    1946 2 8.2 3.5 6 111.8 71
    1946 3 8.8 2 9 32.9 102.9
    1946 4 14.1 6.3 0 29.2 150.5
    1946 5 13.9 6 0 60.7 143.6
    1946 6 17.1 9.5 0 85.3 178.9
    1946 7 20.3 12.8 0 63.9 170.7
    1946 8 18.2 11.1 0 110.2 130.1
    1946 9 17 10.7 0 113 98
    1946 10 11.7 7.5 0 32 47.5
    1946 11 10.2 5.8 0 158.2 22.5
    1946 12 5.8 1.3 6 80.4 45.3


    What i want my program to do is select the first 12 average rainfall stats and work out the average of all of them. and then work out the others. The csv file has 804 rows and 7 columns. each 12 rows represents 1 year.



    Java Code:
    package weatherProgramPackage;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Class1 {
    	
    	
    public static void main(String[] args) throws IOException {
    		
    		// variables
    		String line;
    		String [] parts;
    		
    		// apply array lists to hold string information of columns (titles) for later use
    		// in identification (assigning to values of i)
    		ArrayList<String> year = new ArrayList<String>();
    		ArrayList<String> month = new ArrayList<String>();
    		ArrayList<String> maxTemp = new ArrayList<String>();
    		ArrayList<String> minTemp = new ArrayList<String>();
    		ArrayList<String> afdays = new ArrayList<String>();
    		ArrayList<String> rainmm = new ArrayList<String>();
    		ArrayList<String> sunhrs = new ArrayList<String>();
    		
    		// set up a new file input
    		BufferedReader file = new BufferedReader(new FileReader("sheffielddata.csv"));
    		
    		// while there is still more to read 
    		while((line = file.readLine()) != null) {
    			//splits line into parts
    			parts = line.split(",");
    			
    			// apply for loop to add selected data according 
    			// to index (i and associated values), thus producing rows
    			// from excel with elements identified by column (title)
    			for (int i = 0; i < parts.length; i = i + 7)
    				
    			{
    				//this adds up the identified parts into string arrays;
    				year.add(parts[i]);
    				month.add(parts[i + 1]);
    				maxTemp.add(parts[i + 2]);
    				minTemp.add(parts[i + 3]);
    				afdays.add(parts[i + 4]);
    				rainmm.add(parts[i + 5]);
    				sunhrs.add(parts[i + 6]);
    				
    				ArrayList<String> partsTotal=new ArrayList<String>();
    				partsTotal.add(parts[i]);
    				partsTotal.add(parts[i + 1]);
    				partsTotal.add(parts[i + 2]);
    				partsTotal.add(parts[i + 3]);
    				partsTotal.add(parts[i + 4]);
    				partsTotal.add(parts[i + 5]);
    				partsTotal.add(parts[i + 6]);
    				
    				String[] partstot = partsTotal.toArray(new String[partsTotal.size()]);
    				
    						System.out.println(Arrays.toString(parts));	
    				
    			}
    			
    		}
    		file.close();
    		
    		
    		}
    	}
    i feel that i am now at the stage to index the specific pieces of data but i just don't know how to declare it, if that makes sense.

    I know that once the data is indexed i can than use a double to find the average of the 12 specific pieces of data and so on. once this is complete i should than be able to transfer the data to a simple graph.

    I am sorry if the code is not tagged properly as i am unsure how to do this.

    Any help would be much appreciated thanks .

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Please help been stuck for days now!!

    Well, what you showed is not a CSV file as it has no commas between the values. And I am still not certain where you
    are having problems.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    16
    Rep Power
    0

    Default Re: Please help been stuck for days now!!

    Thanks for the reply..

    the data that i have shown is the first 12 rows of the csv file so an example of it could be shown. i do not know how to identify different parts in the data such as the first 12 rainfall scores and work out an average of them . i am sure that this is the next thing to do in the program but i don't know how to get the information so the averages can be worked out.

    Regards oldskoolbray

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Please help been stuck for days now!!

    If the data is in a CSV then read a single line from the file and split on a comma.

Similar Threads

  1. number of the days (-) days gone past
    By sh4rif in forum New To Java
    Replies: 4
    Last Post: 05-22-2012, 01:17 PM
  2. Replies: 8
    Last Post: 01-06-2011, 06:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •