Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem with executing data from CSV file

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with executing data from CSV file

    I need help and guidance in using data from a csv file to be able to calculate average values from the CSV file.I am trying to calculate the meanAverageTemperature in my files. I have so far written my code to read the files and it is working ok but when it comes to working out the average I am not getting any joy. My files are structured in this format

    2012,41.9,20.5,30.8,125.4,44.9,118.2,99.3,66.7,55. 1,76.5,91.2,105.8
    1798,3.6,4,5.1,10.4,12.9,16.9,16.3,16.4,13.6,9.9,4 .7,1.5

    and I need to read data after the first value in the array because array[0] is the year. Here is my code so far

    package org.com1027.cw2.mm00422;
     
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Stack;
     
     
    public class Data {
     
     
    	private YearlyData[] allYears;
    	//private String precipitationFileName;
    	//private String temperatureFileName;
     
     
    	public Data(String precipitationFileName, String temperatureFileName) {
    		super();
    		this.allYears = new YearlyData[140];
    		//this.precipitationFileName = precipitationFileName;
    		//this.temperatureFileName = temperatureFileName;
     
    		this.parseFile(precipitationFileName, temperatureFileName );
    	}
     
     
    	private void parseFile(String precipitationFileName, String temperatureFileName) {
    	    BufferedReader word_reader = null;
    	    BufferedReader word_reader2 = null;
     
    	    // stack created so that we can use it to ensure that all the readers are closed properly at the end of this method
    	    Stack<BufferedReader> stack1 = new Stack<BufferedReader>();
    	    Stack<BufferedReader> stack2 = new Stack<BufferedReader>();
     
    	    try {
    	      // Attempt to open the files
    	      word_reader = new BufferedReader(new FileReader(precipitationFileName));
    	      word_reader2 = new BufferedReader(new FileReader(temperatureFileName));
     
    	      // readers opened so add to stack
    	      stack1.push(word_reader);
    	      stack2.push(word_reader2);
     
    	      // Read and parse each line from the file
    	      // so create a local variable to hold the line
    	      String line1 = null;
    	      String line2 = null;
     
    	      //here you might need to use indexes for the yearly data array
     
    	      while ((line1 = word_reader.readLine()) != null && (line2 = word_reader2.readLine())!=null ) {
    	        //create the local variables to use;
     
    	        // Split the line in a file
    	       String[] precipValues = line1.split(",");
    	       String[] tempValues= line2.split(",");
     
    	       //Assign first array element to year
    	       int year = Integer.parseInt(precipValues[0]);
    	       double[] precipitation = new double[12];
    	       double[] temperature = new double[12];
     
     
    	        // do any validation checking on the data here before you assign them to code
     
    	        // Make sure we have enough values to parse: i.e. name and 4 years.
    	        if (precipValues.length == 13 && tempValues.length == 13 ){
     
    	           //assign the first array element to the name
    	           String precipYear = precipValues[0];
    	           String tempYear = tempValues[0]; 
     
     
    	          // Remaining values of marks are in the order they are in the file
    	          for (int i = 1; i < precipValues.length; i++) { // Start from 1.
    	        	  precipitation[i-1] = Double.parseDouble(precipValues[i]);
    	        	  temperature[i-1] = Double.parseDouble(tempValues[i]);
    	            //marks[i - 1] = Integer.parseInt(values1[i]);
    	          }
     
     
    	        //StudentMark student = new StudentMark(name,marks);
    	        //so this is like creating a yearly data object
     
    	        System.out.println(line1); // You don't need this line, but it's useful to see what's going on when debugging
    	        System.out.println(line2);
     
    	        // my commenting this.printTest(student);     
    	        //put the object you've just created into the yearlydata array as well
    	        // here we are not doing anything else with that object.
    	        //you will have other methods in your class which need to use the array
    	        //of yearly data objects so you need to store the object that you are
    	        //creating each time you read in a line in the yearly data array.
     
    	    }//end of if
    	      } //end of while
    	    } //end of try
    	    // buffered reader failure
    	    catch (IOException e) {
    	      e.printStackTrace();
    	    }
    	    finally {
    	      // Always close the readers, if they have been opened
    	      this.close(stack1);
    	      this.close(stack2);
    	    }
     
    	  }
     
    	private void close(Stack<BufferedReader> readers) {
        BufferedReader currentReader = readers.pop();
        if (currentReader != null) {
          try {
            currentReader.close();
          }
          catch (IOException e) {
            // if failure to close
            e.printStackTrace();
          }
          finally {
            // if more readers to close then call recursively
            if (!readers.isEmpty()) {
              this.close(readers);
            }
          }
        }
      }
     
     
    	public double calculateMeanPrecipitationMonth(Month month){
    		double sum = 0;
    		for(YearlyData monthPrecipitation : this.allYears){
    			sum += monthPrecipitation.getPrecipitationMonth(month);
    		}
     
    		return sum/this.allYears.length;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Problem with executing data from CSV file

    Did you have any questions? What problems are you having?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with executing data from CSV file

    I need help with how after I have read the csv files I can create a method that will allow me to calculate averages, totals or get the lowest values. So far my code is just reading the file but my methods for calculating the average temperatures line by line are not working. In my code I have put in my method for reading the files and right at the bottom is the method I am trying to use for calculating the Average values from the csv files. I think I might need to create a 2D array because my csv files start with the year first and then the values and I need to calculate the files for year line by line.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Problem with executing data from CSV file

    create a method that will allow me to calculate averages, totals or get the lowest values
    That sounds like you need a loop to go through the data stored in an array.
    Pick one of the tasks and work on it before starting the next one.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with executing data from CSV file

    If you look at the code I posted in the first forum you will see that I have already created a method that allows me to loop through the files line by line and create an array and that is working fine. My problem is my method for calculating the average values line by line is the one that is not working and thats where I need a bit of guidance.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Problem with executing data from CSV file

    method for calculating the average values line by line
    Are the values in an array? Normally values in arrays are processed using a loop.

    What array is the data in? What code have you written to process the data in that array?
    If the data read methods have been tested, you could put them aside and move to working on the code to process the data in the array(s). Define some arrays with a small number of elements for testing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. csv or text file to Multiple csv or text file
    By robert1994 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 19th, 2013, 02:35 PM
  2. Simple Question - How to Parse a CSV File Into Different Data Type Arrays?
    By Obiwan64 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 07:30 PM
  3. Replies: 1
    Last Post: February 27th, 2012, 09:16 AM
  4. Importing data from .csv into array
    By mikeg in forum Collections and Generics
    Replies: 3
    Last Post: November 12th, 2010, 05:59 AM
  5. Cannot update data in .txt/.csv file
    By Azriq007 in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2010, 10:16 PM