I have the following code, which reads from the .csv file and simply displays the output as whatever is being read from the .csv file...(which is test1.csv in the test1.zip attached with this post)

//class to read CSV file :

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;


public class readCSV 

{ //readCSV class starts here 

public static void main(String args[]) throws IOException 

{ //main method starts 

String fName = "test1.csv";//csv file which u wanna read 

String thisLine; //string variable to take each record at a time

int count=0; 

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 
StringTokenizer st =new StringTokenizer(thisLine,",");
while(st.hasMoreElements()){
String field = st.nextToken();
// System.out.print(field+", ");
}
// System.out.println();

}  
}  
} 

I am getting following error while compiling it:-

" C:UsersJaiminDocumentsJCreator ProMyProjectsirrigationreadCSV.java:41: cannot find symbol
symbol  : class StringTokenizer
location: class readCSV
StringTokenizer st =new StringTokenizer(thisLine,",");
^
C:UsersJaiminDocumentsJCreator ProMyProjectsirrigationreadCSV.java:41: cannot find symbol
symbol  : class StringTokenizer
location: class readCSV
StringTokenizer st =new StringTokenizer(thisLine,","); "
                        ^

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Well the error is very simple. You should have figured it out form the compile message. Understanding compile messages is very important if you want to learn programming.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

You import those classes. You must also import the StringTokenizer

import java.util.StringTokenizer

When you are having problems with a class, always check the API. There you will find all the methods of the class and what it can do.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

Also it is recommended by others in this forum to use the spilt method of the String class:
String.split()

Thanks for replying... I din't realize dat simple thing earlier....
nways thanks for your help..

@jpp10 :
The StringTokenizer is legacy class and it's use is not recommended, the java people themselves say this. You can find it in the documentation provided for that class.
Use the split() method of the String class as suggested.

yea even java ppl also dont recommend the use of string tokenizer.
Nways I tried using split method of string class..I made the following modifications in the code and it's working now:

while ((thisLine = myInput.readLine()) != null)
{
String[] result = thisLine.split(",");
for (int x=0; x<result.length; x++)
{ System.out.println(result[x]);


}

Now, my second requirement is to read the data stored in .csv file and process it using some calculations.
I have the code ready for the calculations, but it takes the inputs from the user directly through keyboard entry. However, I wanted to read the values from .csv file.
So, pls guide me on how to start for this

Edited 3 Years Ago by happygeek: fixed formatting

Read the API doc on the Scanner class. It can read from a file just as easily as it can from System.in.

I have two programs, one which reads data from .csv file and other which performs certain calculations based on the data entered by the user through keyboard.

Code for reading data from .csv file :-
//class to read CSV file :

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;


public class readCSV1

{ //readCSV class starts here

public static void main(String args[]) throws IOException

{ //main method starts

String fName = "test1.csv";//csv file which u wanna read

String thisLine; //string variable to take each record at a time

int count=0;


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(",");
for (int x=0; x<result.length; x++)
{ System.out.println(result[x]);

}
System.out.println();

}
}
}

Code for accepting data from keyboard entries and output the results based on certain calculations:

import java.io.*;

public class calculations {


public static void main (String[] args) throws IOException

{
double rootingdepth=0.6;
double asws=125;
double sws;
double allowablecoefficient=0.45;
double mad;


double kc=0.42;

double[] netsoilwaterbalance;
double[] waterreqdforirrigation;
double initialsoilwaterbalance;

int i=0;
int a=0;


System.out.println ("Enter the days of available data");
int n=MyInput.readInt();

double[] rainfall = new double[n];
double[] eto = new double[n];
double[] etc = new double[n];

netsoilwaterbalance = new double[n];
waterreqdforirrigation = new double [n];

sws = rootingdepth*asws;
mad = sws*allowablecoefficient;

initialsoilwaterbalance = mad;


for (i=0;i<n;i++)
{ a=i+1;
System.out.println("Enter the rainfall values for" + a + "days");
rainfall = MyInput.readDouble();

System.out.println("Enter the ETo values for" + a + "days");
eto = MyInput.readDouble();

etc = kc*eto;
}

for (i=0;i<n;i++)
{
a=i+1;

netsoilwaterbalance = initialsoilwaterbalance + rainfall - etc;
System.out.println ("the amount of water in the soil on day" + a + "is" + netsoilwaterbalance);
initialsoilwaterbalance = netsoilwaterbalance;

if (netsoilwaterbalance <= mad)
{ System.out.println ("Irrigation is required on day" + a);

waterreqdforirrigation = mad - netsoilwaterbalance;

System.out.println ("water reqd for irrigation on day" + a + "is" + waterreqdforirrigation);
}
else
{ System.out.println ("Irrigation is not required on day" +a);
}

}

}

}

Now, my requirement is to eliminate the task of getting data from the user (from keyboard) and replace it with reading the same data from .csv file. (Both the rainfall and ETo values are stored in a particular .csv file).
So, pls guide me on how to use both the above codes for satisfying my requirement.

I went through the explanation of scanner class in API documentation. So, instead of using system.in in second code, I have to use scanner and assign some variable to those values read from the file ?? is it the right direction I am thinking in ??

Yes, use the Scanner to read the values from each line in the file and into your variables. I would recommend creating a small data class to replace your individual arrays and use a single ArrayList to hold the instances for each point.

Edit: You could just as easily use your current file reading code with the split() to populate the data instances. You certainly don't have to use the Scanner if you don't want to. It just has some methods that make it more convenient.

do i have to inherit readCSV class into calculations class if i am combining both of them ???

do i have to inherit readCSV class into calculations class if i am combining both of them ???
coz i have to assign variables to the values which i read from .csv file....and use those variables in calculations ...
one way i can think is to create a single class containing constructors and methods for those variables and perform the calculations in the main ...is it the correct way i am thinking ??

and also, i dint understand - "creating a small data class to replace individual arrays and use a single ArrayList to hold the instances for each point". can u elaborate more on that pls

This is a small data structure

class DataPoint{
  public int someValue;
  public float anotherValue;
  public String aLabelPerhaps;
}

These can be kept in a List

List<DataPoint> dataPoints = new ArrayList<DataPoint>();

instead of defining 5 arrays. Each line in your data file is represented by one instance of that data class.

//class to read CSV file :


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;



public class finalcode1


{ //readCSV class starts here


public static void main(String args[]) throws IOException
{


String fName = "data.csv";//csv file which u wanna read


String thisLine; //string variable to take each record at a time


int count=0;
int x=0;
double[] xtra = new double[x];



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(",");
for (x=0; x<result.length; x++)
{ System.out.println(result[x]);
}


for (x=0; x<result.length; x++)
{


xtra[x] = values[x] * 0;


System.out.println("all zeroes" + xtra[x]);
}


}
}


}

The above code read the data from data.csv file (which is also attached herewith for reference), and presents the data. Now, I want to make everything zero and present it...
But, it gives me following error:-

operator * cannot be applied to java.lang.String,int
xtra[x] = result[x] * 0;

I know we cant multiply double with a string...bt the values which result[x] contains are all doubles...so how should i make the result[x] of datatype "double" so as to multiply it with zero.

Edited 3 Years Ago by happygeek: fixed formatting

Or is there any method available in string class which reads only those data which are of datatype double from the file.

There are parsing methods for that in the wrapper classes for the primitives (Integer, Float, Double, etc). Take a look at Double.parseDouble()

One last reminder here: use [code] [/code] tags around any code that you post.
By 15 posts you should know how to use them and this will be the last unformatted code that I'll take the time to read.

Or is there any method available in string class which reads only those data which are of datatype double from the file.

The String class doesn't, but there are methods for that on the Scanner class if you choose to use it to read your file instead.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;


public class finalcode2 

{ //readCSV class starts here 

public static void main (String[] args) throws IOException   
{  fileread();
   double orig_values[] = parseDouble(result[x]);
   system.out.println ("original values" +orig_values[x]);
   zero (orig_values);
      }

   public static void  fileread() throws IOException  
{

String fName = "data.csv";//csv file which u wanna read 

String thisLine; //string variable to take each record at a time

int count=0; 
int x=0;



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(",");
     for (x=0; x<result.length; x++)
        {    
        	
        	 System.out.println(result[x]); 
}


}  
}  

  
 	
 public static double parseDouble(String array[])
 { return double numbers[] = array[];
 }

  	
   	
 public static void zero (double numtozero[])
 {   int y=0;
 	double allzero[] = new double[y];
     for (y=0; y<numtozero.length;y++)
     {
       allzero[y] = numtozero[y] * 0;
   System.out.println ("all zeroes" + allzero[y]);
 } 
 	
 	} 
}

the first method reads the data from .csv file and outputs it
the second method uses parsedouble method for making the string values to double
the third method makes all these values zero.

but, i am getting following errors :-
C:\Users\Jaimin\Documents\JCreator Pro\MyProjects\finalcode2.java:65: '.class' expected
{ return double numbers[] = array[];
^
C:\Users\Jaimin\Documents\JCreator Pro\MyProjects\finalcode2.java:65: illegal start of expression
{ return double numbers[] = array[];
^
C:\Users\Jaimin\Documents\JCreator Pro\MyProjects\finalcode2.java:65: ';' expected
{ return double numbers[] = array[];
^
C:\Users\Jaimin\Documents\JCreator Pro\MyProjects\finalcode2.java:65: illegal start of expression
{ return double numbers[] = array[];
^
4 errors

Following is the code which reads data from file, converts the string to double and then make all those values to zero.

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 output[] = fileread("data2.csv");
    System.out.println("values read from file"+ output);
    /*double orig_values[] = strArray2DblArray (output);*/
   /*system.out.println ("original values" +orig_values[]);*/
   zero (output); 
      }


   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;




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(",");
double 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 zero (double numtozero[])
 {   int y=0;
 	double allzero[] = new double[numtozero.length];
     for (y=0; y<numtozero.length;y++)
     {
       allzero[y] = numtozero[y] * 0;
   System.out.println ("all zeroes" + allzero[y]);
 } 
 	
 	} 
}

But it shows the following error:-

C:\Users\Jaimin\Documents\JCreator Pro\MyProjects\finalcode2.java:66: missing return statement
}

This article has been dead for over six months. Start a new discussion instead.