File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Retrieve Certain Header from CSV file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Retrieve Certain Header from CSV file" Watch "Retrieve Certain Header from CSV file" New topic
Author

Retrieve Certain Header from CSV file

n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28


hye,

Actually, i just want to retrieve certain header field from the Comma Separated file. i can read all the Comma Separated file and i got all the data but i just want some of it only. ,how can i do so?

Urgent.

Thank you

-Nj-
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

What do you mean by Headers? CSV is a plain text file with comma separated values.
If you are talking about the first row of data, you already got the data with you. Or am I misunderstanding your question?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28


hye,

thanks for the reply.

No, you was right.The thing is,, i need to retrieve some column only from the plain text file.
Inside the CSV,,lets say there are things like this;

"Index,Code,Date,Time,Change,List" and i only want to retrieve the data for Index, Code and Date only.

So how can i do it?

p.s: thanks and if possible can you let me know ASAP if you can.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

netra jay wrote:
p.s: thanks and if possible can you let me know ASAP if you can.


http://faq.javaranch.com/java/EaseUp
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

First you said
i can read all the Comma Separated file and i got all the data

and then
"Index,Code,Date,Time,Change,List" and i only want to retrieve the data for Index, Code and Date only.


What is the problem then?

In pseudo code
1) Open a stream to the file
2) Read one line of data
3) Ignore/Drop all unwanted data (Check out StringTokenizer class or String#split methods or even RegularExpressions though I feel regex would be an overkill in this scenario)
4) Process the filtered data
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28

p.s: thanks and if possible can you let me know ASAP if you can.


not being rude but if you think other way around, pardon me.

What i meant was that if you could tell me the way to do it (Explaination),,,not asking you the
code of it.More over, i did mention if you can.

but if you think the former or later...Im sorry because i never know that telling your situation is a wrong doing..





Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did Maneesh's pseudo code answer your question ?


[My Blog]
All roads lead to JavaRanch
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

What gave you the impression I found your post rude?
Your ASAP word attracted the link which I provided. The explanation is in the link.

I have already provided the explanation in my previous post. As for ready made code, we very much discourage it here, especially on the Beginners forum. Best way to learn is code yourself. That is the reason I provided you with the pseudo code
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28


Mr.Maneesh :Anyway,,if it is my fault..I am sorry,Mr.Maneesh.

Thanks for the pseudo code(eventhough I don't get it right but will try my best..;))

Christophe Verr : I dont but you all dont like that kind of answer right?? so i will try to understand

what he actually mean.


NO pain NO gain -Nj-


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I dont but you all dont like that kind of answer right?? so i will try to understand

Not that we don't like it, but we like to encourage our ranchers to think by themselves
Maneesh's 3) should point you in the right direction.
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28


Is it possible to indicate which column i want using string tokenzier?



P.s:you all are really helpful...thanks.

- NJ-
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Is it possible to indicate which column i want using string tokenzier?

No. You have to go through each element via nextElement or nextToken. Instead you could use String#split which returns an array. This would be easier to access each element by its index.
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28

hye.

instead of using that method i used scanner and i get the column that i want.
But the problem, is it does not print the value of the specific column.




i think i miss something in the code..thanks guys for the helpful reply.
i learn a lot.

-Njay-
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

netra jay wrote:
i think i miss something in the code..

You think correct.
String strar []=i.split(";");

Shouldnt that be a comma (,) instead of a semicolon (;) ?
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28


thanks..

Shouldnt that be a comma (,) instead of a semicolon (;) ?


if i put comma i get all the column which i dont want(Note:there are no value of the column to).but when i put semicolon i get the specific column that i want.But, the value for the column is missing?





-Njay-
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You told us that a line looks like "Index,Code,Date,Time,Change,List". So, you split it with ",". If you use ";" and your data is not delimited with ";", you'll get nothing.
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28

what if the delimiters is semicolon instead of comma like

Index;Code;Date;Time;Change;List


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Then you would use ";" to split the string.

myself wrote:If you use ";" and your data is not delimited with ";", you'll get nothing.

I'd like to rectify that. You'll get the whole line.
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28


but i dnt get the whole line..eventhough i use semicolon as my delimiters.

wonder why???
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

netra jay wrote:wonder why???

System.out.println(i);
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28

System.out.println(i);

when i put this statement,i got the whole column title only.
for example,,in my csv file contain data like:

IndexCode;Date;Time;Change;List
FBM;04/04/2009;;UPD
FDG;04/04/2009;;UTP
FTS;04/04/2009;;TUR
FDE;04/04/2009;;RET

SO, when i use this code


i get the result like this:

Result:
IndexCode Date Change

which is without the value of the column.



Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

It's a good progress. Now you have to loop until you have read every lines of the file. Check Scanner#hasNext.
n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28



Should i loop it inside the for loop or outside it?

could you give me any example on using scanner hasnext()??

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
This topic demonstrates numerous reasons why it's better to use a pre-made CSV library instead of trying to roll your own. See the http://faq.javaranch.com/java/AccessingFileFormats page for details.


Ping & DNS - my free Android networking tools app
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Actually you should use hasNextLine() instead. Sorry.

The algorithm is :
1. Read one line from file until end of file
2. Split values
3. Show values

You'll find usage examples of Scanner all over the web. Here is one place.

n jay
Greenhorn

Joined: Dec 29, 2009
Posts: 28


my code is like this



ok i use has next and i get the result like this:


the result:

IndexCode
FBMAPU
FBMAPU
FBMAPM
FBMAPU
PrevClose
Change
Currency

what did i miss???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieve Certain Header from CSV file