• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retrieve Certain Header from CSV file

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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-
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
n jay
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..





 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did Maneesh's pseudo code answer your question ?
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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



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

- NJ-
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what if the delimiters is semicolon instead of comma like

Index;Code;Date;Time;Change;List



 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

wonder why???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

netra jay wrote:wonder why???


System.out.println(i);
 
n jay
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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

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

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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???
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic