Results 1 to 3 of 3
  1. #1
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default To populate the data processed into a .csv or excel file

    I have made the following code, which reads rainfall and ETo values from 2 separate .csv files. and then calculates the irrigation requirement, and the tank size needed per day.

    Now I want to output the processed (calculated) data (which is irrigation requirement and tank size needed per day) into an excel sheet or any .csv file.

    so, pls help me how should I proceed

    Java Code:
    /**
     * @(#)finalcode1.java
     *
     *
     * @author 
     * @version 1.00 2009/2/13
     */
    
    
    //class to read CSV file :
    
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.*;
    
    
    public class finalcode2 
    
    {  
    
    public static void main (String[] args) throws IOException   
    {  
       
        double rainfall[] = fileread("rainfall.csv");
         for (int k=0; k<rainfall.length; k++)
         	
         { System.out.println("rainfall values read from file "+ rainfall[k]);
         }
    
         double ETo[] = fileread("evapotranspiration.csv");
          for (int z=0; z<ETo.length; z++)
          { System.out.println("ETo values read from file "+ ETo[z]);
          }
        /*double orig_values[] = strArray2DblArray (output);
        system.out.println ("original values" +orig_values[]);*/
        calculations (rainfall, ETo); 
          }
    
    
       public static double[] fileread(String fName) throws IOException  
    {
    
    String thisLine; //string variable to take each record at a time
    
    int count=0; 
    int x=0;
    
    
    
    double dblArray[] = null;
    
    FileInputStream fis = new FileInputStream(fName); 
    //A FileInputStream obtains input from a file 
    
    DataInputStream myInput = new DataInputStream(fis);
    /*data input stream lets an application read primitive Java data types 
    from an underlying input stream*/
    
    
    
    while ((thisLine = myInput.readLine()) != null)
    { //beginning of outer while loop 
    
    String[] result = thisLine.split(",");
    dblArray = new double[result.length];
         for (x=0; x<result.length; x++)
         	
            {    	/*return result[x];*/ 
            	dblArray[x] = Double.parseDouble(result[x]);
            	
            		
             }
    
    } 
     return dblArray;
     	
    }  
    
      
    /* public static double [] strArray2DblArray(String [] strArray)
    {
      double [] dblArray = new double[strArray.length]; //create double Array with same sizas as the string array
      for (int i = 0; i<strArray.length;i++)
         {
          dblArray[i] = Double.parseDouble(strArray[i]);
         }
     return dblArray;
    }*/
      	
       	
     public static void calculations (double rain[], double evaptrans[])
     {   int a=0;
         double rootingdepth=0.6;
         double asws=125;
         double sws;
         double allowablecoefficient=0.45;
         double mad;
         double kc=0.42;
         double etc[] = new double [evaptrans.length];
         sws = rootingdepth*asws;
         mad = sws*allowablecoefficient;
         System.out.println ("mad= "+mad);
         double initialsoilwaterbalance = mad;
         double[] netsoilwaterbalance = new double [rain.length];
         double[] waterreqdforirrigation = new double [rain.length];
         
         double[] eff_rain = new double [rain.length];
         double catchmentcoeff_per_m2 = 0.8;
         double[] tank_vol = new double [rain.length];
         
         double sum = 0;
          
         
         for (int j=0; j<evaptrans.length; j++)
         {  etc[j] = evaptrans[j] * kc;
            eff_rain[j] = rain[j] * catchmentcoeff_per_m2;
         }
         
         
          for (int i=0;i<rain.length;i++)
             { 
                 a=i+1;
               
               netsoilwaterbalance[i] = initialsoilwaterbalance + rain[i] - etc[i];
               System.out.println ("the amount of water in the soil on day " + a + " is " + netsoilwaterbalance[i]);
               initialsoilwaterbalance = netsoilwaterbalance[i];
               
                 if (netsoilwaterbalance[i] <= mad)
                     { System.out.println ("Irrigation is required on day " + a);
                        
                        waterreqdforirrigation[i] = mad - netsoilwaterbalance[i];
                        
                          System.out.println ("water reqd for irrigation on day " + a + " is " + waterreqdforirrigation[i]);
                          
                          tank_vol[i] = eff_rain[i] + waterreqdforirrigation[i];
                          System.out.println ("\t tank size needed on day " + a + " is " + tank_vol[i]);
                           }
                 else 
                      { System.out.println ("Irrigation is not required on day " +a);
                        tank_vol[i]= eff_rain[i];
                        System.out.println ("\t tank size needed on day " + a + " is " + tank_vol[i]);
                           }
    
              }
     	   
     	 for (int n=0; n<rain.length;n++)
     	 	
     	 { 
     	    sum += tank_vol[n];
     	 }
     	  double avg;
     	   avg = sum/rain.length;
     	   
     	   System.out.println ("\t\t optimal tank size required " +avg);
     	  	
     	  }
     	
     	}

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Firstly, there's no need for a DataInputStream as all you're doing is readLine() - a method from BufferedInputStream.

    Secondly, creating the output is just as easy as input. Just reverse the process you use for input - simple with an OutputStream and String.format()

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Additionally, I've just found a handy library for csv i/o. Googling for JavaCsv should bring it up.

Similar Threads

  1. populate jCombobox with database data
    By joeyxaza in forum JDBC
    Replies: 2
    Last Post: 01-06-2014, 10:09 AM
  2. populate jCombobox with database data
    By joeyxaza in forum JDBC
    Replies: 0
    Last Post: 01-19-2009, 04:30 PM
  3. Recovering data from Excel spreadsheet
    By Anubis in forum SWT / JFace
    Replies: 0
    Last Post: 12-17-2008, 08:28 PM
  4. how to populate data in drop-down box
    By ma-la in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-01-2008, 11:24 AM
  5. How to read data from excel and store it in db
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-03-2008, 10:19 AM

Posting Permissions

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