I would like to know any advice for creating a program that will read in a *.csv file and load the data into a 24 x 24 array for further processing. I am including a total of 48 records, so that you can see how the data looks and what I'll be working with.

1.000000,0.767801,0.370782,1.887500,0.817261,0.120824,0.178702,1.329200,0.128401,0.022936,0.009620,0.263158,0.088230,0.702998,0.160940,0.607977,0.169635,0.000956,0.009970,0.147252,0.856678,0.031328,0.025595,0.000522
1.302420,1.000000,0.482914,2.458320,1.064420,0.157364,0.232745,1.731180,0.167232,0.029872,0.012529,0.342742,0.114913,0.915598,0.209611,0.791841,0.220936,0.001245,0.012985,0.191783,1.115750,0.040803,0.033336,0.000680

Recommended Answers

All 15 Replies

I don't know what a csv file is or what the structure is, but if its just a text a file with numbers like that, it's quite simple.

The only thing to be careful about in this code is that it assumes there won't be more than 24 elements, or tokens, in each line of text.

String[24][24] numbers;
 
File file = new File("something.csv");
 
BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
 
//read each line of text file
while((line = bufRdr.readLine()) != null)
{
	StringTokenizer st = new StringTokenizer(line,",");
	while (st.hasMoreTokens())
	{
		//get next token and store it in the array
		numbers[row][col] = st.nextToken();
		col++;
	}
	row++;
}
 
//close the file
bufRdr.close();

Thanks for the help. I only had to change the program slightly with changes to the logic for it to work properly. (Please see my code to see what I did).

String [][] numbers = new String [24][24];
 
	File file = new File("Currency Exchange Rates.csv");
 
	BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
	String line = null;
	int row = 0;
	int col = 0;
 
	//read each line of text file
	while((line = bufRdr.readLine()) != null && row < 24)
	{	
	StringTokenizer st = new StringTokenizer(line,",");
	while (st.hasMoreTokens())
	{
		//get next token and store it in the array
		numbers[row][col] = st.nextToken();
		col++;
	}
	col = 0;
	row++;
	}

:)

I don't know what a csv file is or what the structure is, but if its just a text a file with numbers like that, it's quite simple.

The only thing to be careful about in this code is that it assumes there won't be more than 24 elements, or tokens, in each line of text.

String[24][24] numbers;
 
File file = new File("something.csv");
 
BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
 
//read each line of text file
while((line = bufRdr.readLine()) != null)
{
	StringTokenizer st = new StringTokenizer(line,",");
	while (st.hasMoreTokens())
	{
		//get next token and store it in the array
		numbers[row][col] = st.nextToken();
		col++;
	}
	row++;
}
 
//close the file
bufRdr.close();

String field[] = line.split(",");

commented: Wasn't worth the 4 year wait. Quote an entire post, then one useless line -7

@chackboom

Am pretty sure the O.P. must have found a solution to it long long ago, please check the dates before posting on such fossilized threads.

corrections in fossilized threads is certainly needed becuase those are still the source of information for many.

commented: So you spotted that after three years -1

The previous example breaks after col 25 because the array size reaches it limit.Here is a solution that does not depend on the arraysize

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.StringTokenizer;


public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileName="C:/Documents and Settings/tntadmin/workspace2/a.csv";
		try {
			BufferedReader br = new BufferedReader( new FileReader(fileName));
			String strLine = null;
			StringTokenizer st = null;
			int lineNumber = 0, tokenNumber = 0;
 
			while( (fileName = br.readLine()) != null)
			{
				lineNumber++;
 
				//break comma separated line using ","
				st = new StringTokenizer(fileName, ",");
 
				while(st.hasMoreTokens())
				{
					//display csv values
					tokenNumber++;
					System.out.println("Line # " + lineNumber + 
							", Token # " + tokenNumber 
							+ ", Token : "+ st.nextToken());
				}
 
				//reset token number
				tokenNumber = 0;
 
			}
		} 
		
		
		
		
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

Here is my pimp code :)

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class CSVFileReader {

	String fileName;
	
	 ArrayList <String>storeValues = new ArrayList<String>();
	public CSVFileReader(String FileName)
	{
		this.fileName=FileName;
	}
	
	public void ReadFile()
	{
		try {
			//storeValues.clear();//just in case this is the second call of the ReadFile Method./
			BufferedReader br = new BufferedReader( new FileReader(fileName));
		
			StringTokenizer st = null;
			int lineNumber = 0, tokenNumber = 0;
 
			while( (fileName = br.readLine()) != null)
			{
				lineNumber++;
				System.out.println(fileName);
				storeValues.add(fileName);
				//break comma separated line using ","
				st = new StringTokenizer(fileName, ",");

				while(st.hasMoreTokens())
				{	
					
					System.out.println("Line # " + lineNumber + 
							", Token # " + tokenNumber 
							+ ", Token : "+ st.nextToken());
					
				}
 
				//reset token number
				tokenNumber = 0;
				
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	
	//mutators and accesors 
	public void setFileName(String newFileName)
	{
		this.fileName=newFileName;
	}
	public String getFileName()
	{
		return fileName;
	}
	public ArrayList getFileValues()
	{
		return this.storeValues;
	}
	public void displayArrayList()
	{
		for(int x=0;x<this.storeValues.size();x++)
		{
			System.out.println(storeValues.get(x));
		}
	}
	
}
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileName="C:/Documents and Settings/tntadmin/workspace2/a.csv";
		CSVFileReader x=new CSVFileReader(fileName);
		x.ReadFile();
		x.displayArrayList();
	
	}

}

Feel free to comment and tell me how to make this better.

Is there a way to print only a single line. Let's say that the a.csv files has 50 lines, how can I chose only line 20?

just read the whole file into an array then echo the individual line you want. I'm a PHP programmer learning Java but I imagine the method is similar. (For those who look at this in the future)

//print just row 10
echo array[10];

new StringTokenizer(fileName, ",")

The problem with the above imlementation is that it uses StringTokenizer(fileName, ",") to break the line (initially) into a bunch of chunks. This approach does not work when there are commas is the string values. In that case, commas within the quotes have to be ignored, which the tokenizer does not do.

Let me refer you to a single class implementation that reads and write CSV files: The Only Class You Need for CSV Files:

http://agiletribe.wordpress.com/2012/11/23/the-only-class-you-need-for-csv-files/

A bit late for the O.P. (!), but may be useful to someone searching this topic, so thank you.

I am super new to java as well and still confused about the strings being immutable concept, doesn't this:

fileName = br.readLine()

in a loop create a lot of overhead?

commented: start your own thread rather than hijacking another -3

Strings are immutable - in other words once a String object has been created there is no way to change that String.
Don't confuse String objects with String variables.
A String variable holds a reference to a String object. You can think of it as a pointer to a String object, if you are familiar with pointers. String variables can be updated to refer to a different String object, in which case the object it referred to previously may become eligible for garbage collection.
String var = "hello"; // "hello" is an immutable String object. var holds a reference to it
var = "World"; // "World is an immutable String object. var now refers to it. The "Hello" object may be garbage collected

Now for the loop:
br.readLine() // reads a line of info from the imput stream and uses that to create a new String object
fileName = br.readLine() // the variable fileName is updated to refer to that object.

There's nothing special about the overhead of that code. String objects are used so much that the Java byte code and Virtual Machine are highly optimised for handling them. Updating the variable is trivial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.