Results 1 to 3 of 3
- 02-20-2009, 11:05 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 24
- Rep Power
- 0
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); } }
- 02-21-2009, 05:17 AM #2
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()
- 02-27-2009, 12:56 AM #3
Similar Threads
-
populate jCombobox with database data
By joeyxaza in forum JDBCReplies: 2Last Post: 01-06-2014, 11:09 AM -
populate jCombobox with database data
By joeyxaza in forum JDBCReplies: 0Last Post: 01-19-2009, 05:30 PM -
Recovering data from Excel spreadsheet
By Anubis in forum SWT / JFaceReplies: 0Last Post: 12-17-2008, 09:28 PM -
how to populate data in drop-down box
By ma-la in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-01-2008, 12:24 PM -
How to read data from excel and store it in db
By saran123 in forum New To JavaReplies: 5Last Post: 10-03-2008, 11:19 AM
Bookmarks