469,314 Members | 2,238 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,314 developers. It's quick & easy.

read csv file and make array

13
Hello,

I am very new to JAVA.

I would like to know, how to read *.csv file from java and how to save that data into an array.

regards,
bela
Sep 5 '07 #1
22 20310
madhoriya22
252 100+
Hello,

I am very new to JAVA.

I would like to know, how to read *.csv file from java and how to save that data into an array.

regards,
bela
Hi,
First download the javacsv.jar. After that read a bit about csvReader API.
http://opencsv.sourceforge.net/api/a...CSVReader.html
Sep 5 '07 #2
dmjpro
2,476 2GB
Hi,
First download the javacsv.jar. After that read a bit about csvReader API.
http://opencsv.sourceforge.net/api/a...CSVReader.html

Your answer is really Impressive.
As soon as I saw this I went through Test it.
But failed.
Actually what happened, I downloaded that Jar and made my NetBeans identifies that Jar.
But this line is not working as mentioned in your Link.
Expand|Select|Wrap|Line Numbers
  1. import au.com.bytecode.opencsv.*;
  2.  
Please help.

Kind regards,
Dmjpro.
Sep 5 '07 #3
dmjpro
2,476 2GB
Your answer is really Impressive.
As soon as I saw this I went through Test it.
But failed.
Actually what happened, I downloaded that Jar and made my NetBeans identifies that Jar.
But this line is not working as mentioned in your Link.
Expand|Select|Wrap|Line Numbers
  1. import au.com.bytecode.opencsv.*;
  2.  
Please help.

Kind regards,
Dmjpro.
Sorry I got it.
Actually what it mentioned in your link that the CSVReader class is under
au.com.bytecode.opencsv package.
But what I saw in NetBeans is, com.csvreader.
Anyway, I think it is Jar specific.

Kind regards,
Dmjpro.
Sep 5 '07 #4
madhoriya22
252 100+
Your answer is really Impressive.
As soon as I saw this I went through Test it.
But failed.
Actually what happened, I downloaded that Jar and made my NetBeans identifies that Jar.
But this line is not working as mentioned in your Link.
Expand|Select|Wrap|Line Numbers
  1. import au.com.bytecode.opencsv.*;
  2.  
Please help.

Kind regards,
Dmjpro.
Hi,
I think u have not put the jar at right place. Put that jar in Libraries folder of ur project. That line should work. U can try with this line also
Expand|Select|Wrap|Line Numbers
  1.  
  2. import com.csvreader.CsvReader;
  3.  
and use the methods given this class to read the CSV file.
Sep 5 '07 #5
dmjpro
2,476 2GB
Hi,
I think u have not put the jar at right place. Put that jar in Libraries folder of ur project. That line should work. U can try with this line also
Expand|Select|Wrap|Line Numbers
  1.  
  2. import com.csvreader.CsvReader;
  3.  
and use the methods given this class to read the CSV file.
I think you didn't see my Last Post.
I posted my solution there.
Actually, I used the package what is mentioned in your link.
Check that link.

Kind regards,
Dmjpro.
Sep 5 '07 #6
madhoriya22
252 100+
I think you didn't see my Last Post.
I posted my solution there.
Actually, I used the package what is mentioned in your link.
Check that link.

Kind regards,
Dmjpro.
Hi,
Check with this link again ....
http://javacsv.sourceforge.net/com/c...e-summary.html
This is the different and right one.
Sep 5 '07 #7
JosAH
11,448 Expert 8TB
I think you didn't see my Last Post.
I posted my solution there.
Actually, I used the package what is mentioned in your link.
Check that link.

Kind regards,
Dmjpro.
Just for the fun of it I downloaded and installed the .jar and api docs. After reading
the API docs and the example code (AddressExample.java: the demo reads
the addresses.csv file) I decided to add the .jar to my classpath and it just
worked (as it should). Please read before you simply click away.

kind regards,

Jos
Sep 5 '07 #8
r035198x
13,262 8TB
Just for the fun of it I downloaded and installed the .jar and api docs. After reading
the API docs and the example code (AddressExample.java: the demo reads
the addresses.csv file) I decided to add the .jar to my classpath and it just
worked (as it should). Please read before you simply click away.

kind regards,

Jos
Done it too.
The reading part didn't kill me either.
Sep 5 '07 #9
JosAH
11,448 Expert 8TB
Done it too.
The reading part didn't kill me either.
Of course not; why should it? The magic word is reading again.

kind regards,

Jos
Sep 5 '07 #10
bela
13
Hi,
First download the javacsv.jar. After that read a bit about csvReader API.
http://opencsv.sourceforge.net/api/a...CSVReader.html

thank you.
But I'm not able to convert in / store as in array form.

regards,
bela
Sep 5 '07 #11
madhoriya22
252 100+
thank you.
But I'm not able to convert in / store as in array form.

regards,
bela
Hi,
Show us a bit what u have tried ? Then only we vil be able to help you.
Sep 5 '07 #12
Hi, I tried an Example but am not able to read the file.
My Code is,

mport com.csvreader.CsvReader;
import java.io.*;
class csveg
{
public static void main(String a[])
{
try
{
CsvReader cs=new CsvReader("hi.csv");
System.out.println("hiii "+ cs);
System.out.println("hiii "+ cs.getColumnCount());//Prints 0
String arra[]=cs.getValues();
System.out.println("hiii "+arra.length);//Prints 0
for(int j=0;j<arra.length;j++)
{
System.out.println("hiii "+j);
System.out.println(arra[j]);
}


}catch(Exception e){System.out.println("Exception is ");}
}
}

pls help...
Sep 5 '07 #13
madhoriya22
252 100+
Hi, I tried an Example but am not able to read the file.
My Code is,

mport com.csvreader.CsvReader;
import java.io.*;
class csveg
{
public static void main(String a[])
{
try
{
CsvReader cs=new CsvReader("hi.csv");
System.out.println("hiii "+ cs);
System.out.println("hiii "+ cs.getColumnCount());//Prints 0
String arra[]=cs.getValues();
System.out.println("hiii "+arra.length);//Prints 0
for(int j=0;j<arra.length;j++)
{
System.out.println("hiii "+j);
System.out.println(arra[j]);
}


}catch(Exception e){System.out.println("Exception is ");}
}
}

pls help...
Hi,
If u hav not read the API for method getColumnCount() .. then check it again and try understand what it is saying ....
It is saying that "Gets the count of columns found in this record".

Here U hav get the file in the csvReader but havn't started reading it ... So start reading the file then only this method vil give u the column count for the particular record u r reading.

I hope u r getting my point.
Sep 5 '07 #14
bela
13
Hi,
Show us a bit what u have tried ? Then only we vil be able to help you.
yes, my code is :

import java.io.*;
import au.com.bytecode.opencsv.CSVReader;

public class csv2array {

private static final String data = "e:\\weather.csv";

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

CSVReader reader = new CSVReader(new FileReader(data),',','\'',1);
String [] nextLine;
int cnt=0;
String[][] str1= new String[14][5];

while ((nextLine = reader.readNext()) != null) {
System.out.println("Row no is : " + cnt);
System.out.println(nextLine[0]+',' + nextLine[1]+','+ nextLine[2]+',' +nextLine[3]+','+nextLine[4]);
for(int n=0;n<14;n++){
for(int rowelement=0;rowelement<5;rowelement++){
str1[n][rowelement]=nextLine[rowelement];
}
}
cnt++;
}

}
}

weather.csv is available on WEKA.
I'm taking data in matrix str1.
How to see the containts of str1?
when I tried, System.out.println(str1); it gives address of str1.

regards,
bela
Sep 6 '07 #15
madhoriya22
252 100+
yes, my code is :

import java.io.*;
import au.com.bytecode.opencsv.CSVReader;

public class csv2array {

private static final String data = "e:\\weather.csv";

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

CSVReader reader = new CSVReader(new FileReader(data),',','\'',1);
String [] nextLine;
int cnt=0;
String[][] str1= new String[14][5];

while ((nextLine = reader.readNext()) != null) {
System.out.println("Row no is : " + cnt);
System.out.println(nextLine[0]+',' + nextLine[1]+','+ nextLine[2]+',' +nextLine[3]+','+nextLine[4]);
for(int n=0;n<14;n++){
for(int rowelement=0;rowelement<5;rowelement++){
str1[n][rowelement]=nextLine[rowelement];
}
}
cnt++;
}

}
}

weather.csv is available on WEKA.
I'm taking data in matrix str1.
How to see the containts of str1?
when I tried, System.out.println(str1); it gives address of str1.

regards,
bela
Hi,
If u hav done all this hard work urself then printing array values should not be a problem :) ur str1 is a two dimensional array. For reading values in that u hav to put two for loops like..
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 14; i++) {
  2.     for(int j = 0; j < 5; j++) {//obviously u can get row and column lenghts by using str1.length.
  3.      System.out.println(str1[i][j]);
  4.     }
  5. }
  6.  
Sep 6 '07 #16
bela
13
Hi,
If u hav done all this hard work urself then printing array values should not be a problem :) ur str1 is a two dimensional array. For reading values in that u hav to put two for loops like..
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 14; i++) {
  2.     for(int j = 0; j < 5; j++) {//obviously u can get row and column lenghts by using str1.length.
  3.      System.out.println(str1[i][j]);
  4.     }
  5. }
  6.  
yes, this I've tried, and also know this method. But here it prints element by element. my final aim is to make new matrix by extracting few rows and column of this str1 matrix, where I need entire row/ entire column.
using for loops, I can do it,
but I was searching for different method if there is as this will be lengthy procedure.
I have to create those many arrays/matrices.
Do you know any other method?
Sep 6 '07 #17
Hi,
If u hav not read the API for method getColumnCount() .. then check it again and try understand what it is saying ....
It is saying that "Gets the count of columns found in this record".

Here U hav get the file in the csvReader but havn't started reading it ... So start reading the file then only this method vil give u the column count for the particular record u r reading.

I hope u r getting my point.
Ya got it.. Now its executing.. Thnks
Sep 6 '07 #18
madhoriya22
252 100+
Ya got it.. Now its executing.. Thnks
Hi,
Ofcourse Welcome Buddy :)
Sep 6 '07 #19
madhoriya22
252 100+
yes, this I've tried, and also know this method. But here it prints element by element. my final aim is to make new matrix by extracting few rows and column of this str1 matrix, where I need entire row/ entire column.
using for loops, I can do it,
but I was searching for different method if there is as this will be lengthy procedure.
I have to create those many arrays/matrices.
Do you know any other method?
Hi,
Check my post #7. Go to that link. That CsvReader API has more methods than the one which u are using. By Using methods of this u can skip the records which u don't want to read.

Tip: Dont be rude while asking, sharing or answering. Always be thankful :)
Sep 6 '07 #20
bela
13
Hi,
Check my post #7. Go to that link. That CsvReader API has more methods than the one which u are using. By Using methods of this u can skip the records which u don't want to read.

Tip: Dont be rude while asking, sharing or answering. Always be thankful :)
hello,

thank you.
I'll go through it.

regards,
bela
Sep 6 '07 #21
Hi,
Ofcourse Welcome Buddy :)
one more query :-)

I used parse mtd of CsvReader and created a new CsvReader obj.
If i want to write this to a file, what should i do??

pls help...
Sep 6 '07 #22
madhoriya22
252 100+
one more query :-)

I used parse mtd of CsvReader and created a new CsvReader obj.
If i want to write this to a file, what should i do??

pls help...
Hi,
Again U have to read a bit for that :) Use CsvWriter class for that. Here is the link for that class API...
http://javacsv.sourceforge.net/com/c...CsvWriter.html
start using methods of it and have fun :)
Sep 6 '07 #23

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Chris | last post: by
2 posts views Thread by Jim Richards | last post: by
19 posts views Thread by ranjeet | last post: by
7 posts views Thread by Naren | last post: by
6 posts views Thread by comp.lang.php | last post: by
9 posts views Thread by Adi | last post: by
3 posts views Thread by KWienhold | last post: by
6 posts views Thread by xdeath | last post: by
11 posts views Thread by a | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.