473,765 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

read csv file and make array

13 New Member
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
22 20745
bela
13 New Member
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 Contributor
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
snowfall
56 New Member
Hi, I tried an Example but am not able to read the file.
My Code is,

mport com.csvreader.C svReader;
import java.io.*;
class csveg
{
public static void main(String a[])
{
try
{
CsvReader cs=new CsvReader("hi.c sv");
System.out.prin tln("hiii "+ cs);
System.out.prin tln("hiii "+ cs.getColumnCou nt());//Prints 0
String arra[]=cs.getValues() ;
System.out.prin tln("hiii "+arra.leng th);//Prints 0
for(int j=0;j<arra.leng th;j++)
{
System.out.prin tln("hiii "+j);
System.out.prin tln(arra[j]);
}


}catch(Exceptio n e){System.out.p rintln("Excepti on is ");}
}
}

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

mport com.csvreader.C svReader;
import java.io.*;
class csveg
{
public static void main(String a[])
{
try
{
CsvReader cs=new CsvReader("hi.c sv");
System.out.prin tln("hiii "+ cs);
System.out.prin tln("hiii "+ cs.getColumnCou nt());//Prints 0
String arra[]=cs.getValues() ;
System.out.prin tln("hiii "+arra.leng th);//Prints 0
for(int j=0;j<arra.leng th;j++)
{
System.out.prin tln("hiii "+j);
System.out.prin tln(arra[j]);
}


}catch(Exceptio n e){System.out.p rintln("Excepti on 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 New Member
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.CSVRea der;

public class csv2array {

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

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.prin tln("Row no is : " + cnt);
System.out.prin tln(nextLine[0]+',' + nextLine[1]+','+ nextLine[2]+',' +nextLine[3]+','+nextLine[4]);
for(int n=0;n<14;n++){
for(int rowelement=0;ro welement<5;rowe lement++){
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.prin tln(str1); it gives address of str1.

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

import java.io.*;
import au.com.bytecode .opencsv.CSVRea der;

public class csv2array {

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

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.prin tln("Row no is : " + cnt);
System.out.prin tln(nextLine[0]+',' + nextLine[1]+','+ nextLine[2]+',' +nextLine[3]+','+nextLine[4]);
for(int n=0;n<14;n++){
for(int rowelement=0;ro welement<5;rowe lement++){
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.prin tln(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 New Member
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
snowfall
56 New Member
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 Contributor
Ya got it.. Now its executing.. Thnks
Hi,
Ofcourse Welcome Buddy :)
Sep 6 '07 #19
madhoriya22
252 Contributor
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

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

Similar topics

8
17088
by: Chris | last post by:
Can anybody help. I need to read a txt file backwords line by line. Can anybody help me do this. Thanks Chris
2
3231
by: Jim Richards | last post by:
I have been told by a local PC club technician that 98SE cannot read NTFS drives in a network. Is this true? TIA, Jim.
19
2522
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820, 1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902, 2070, 3025, 4333, 5854, 7805, 9231, 10597, 16047........................... soo onnnnnn
7
2264
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
6
2332
by: comp.lang.php | last post by:
if (!function_exists('bigfile')) { /** * Works like file() in PHP except that it will work more efficiently with very large files * * @access public * @param mixed $fullFilePath * @return array $lineArray * @see actual_path */
9
5211
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
3
3379
by: KWienhold | last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and C#) that stores files and additional information in a single compound file using IStorage/IStream. Since files in a compound file aren't really useful to a user, I use the IStream::Read function together with a StreamWriter to extract single files from my compound document. When I first tested these functions everything seemed to work fine (and basically, it does),...
6
2058
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is supposed to inherit those from it. I have done so, but now, it requires that all the data be stored in an array. The final program, should be able to : 1) Add new taxi's into the program (which retains even after program exit) 2) Search for a...
14
11642
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the correct way to get it into a memory stream. Any help appreciated.
11
1621
by: a | last post by:
To solve the 100k * 100k data, I finally adopt the file read method and use malloc. The system somehow knows to use virtual memory. Now I first test 1k * 1k data but something uninterpretable happens again. The data file starts with: 42.106983132 85.931514337 98.155213938 23.685776453 76.827067592 ....
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.