This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Getting certain information out of a .csv file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting certain information out of a .csv file" Watch "Getting certain information out of a .csv file" New topic

Getting certain information out of a .csv file

Peter Read

Joined: Apr 21, 2004
Posts: 7
Hello im very new to java, i was wondering whether someone out there could help me with a problem that i have.
I have to get a cetain piece of information out of a .csv file i need to get information from a coloumn in the file. The information that i need is in the same place throughout the file seperated by comma's.
So far ive managed to ope the file and get it to display the information to the screen, Ive been looking for about 2 days for the soloution to this problem but with no luck. I think it can either be solved by reqular expressions or by tokens but i can find a nice simple tutorial to help me.
Im very sorry if this isnt a Beginner subject but to me its really hard, any help on this subject would be very much apreacated.
Oh also i need to store the information so i can put it into a frequency table.
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
i am not sure about the extension file type your talking about. but if the file looks like
column1, column2, column3 etc etc
then a string tokenizer will definately do the trick. You say you have the code to open and read the file, so i am presuming this is through some sort of a loop construct -with a temporary string being used to get the value of readLine() or some such method.
so for the tokenizer part:
presuming 'input' is the string assigned to the readLine() method, in other words your temporary variable in your file reader loop..

For your purpose, you should look into the nextToken() method. As you have described, you might be after the 3rd colomn of every line. So obviously you will pass through 2 tokens before reaching 'here':
token 1 , token 2 , HERE
will move the token that its 'pointing' to, one to the right. Obviously with each token being seperated by a delimiter commer.
hope this helps somewhat, below is a link to the StringTokenizer class from the java api.
[ April 21, 2004: Message edited by: Antoine Waugh ]
[ edited to break long line -ds ]
[ April 21, 2004: Message edited by: Dirk Schreckmann ]

B.C.S.T, SCJP, Hero
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
'The same place throughout the file' meaning the same column when viewed in a spreadsheet program?
If that's the case, I'd recommend using String.split to obtain an array of which you can reference only the element you want. With StringTokenizer, you'll end up looping through an Enumeration just to extract the nth element.
Peter Read

Joined: Apr 21, 2004
Posts: 7
I'd Just like to say thanks to ray and Antoine for the quick replies
As for the code it does work however it displays all the information in the file where as i only need one coloumn of the information,
for example the file is like this
atm ive got the program to read through and store all this information however i only need the information contained in example3
all the information goes down in sink so there are a set amount of commas between the different points of information. it seems the code supplied by Antoine goes through the whole the file and stores all the information as when i get it to display to the screen it simply displays all of it.
Can i change the current code from Antoine to just store and grab the information i need from example3. Or will i need to use something different?
Any more help would be really greatful
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
>>atm ive got the program to read through and store all this information however i only need the information contained in example3
what the stringTokenizer does is collect whatever information you want.
for example, if you do

then all your doing is storing 'whatever' to have the contents of that particular token.
so basically you have two suggestions,
rays >> where you use his "String.split to optain an array of which you can refernce .."
or if you choose the tokenizer method, ensure you do not store all of the data in the program -as this is unnecessary. All you want (as you have specified) is the certain column 3.
so in your loop only read that one token, and append it to maybe an array, or some sort of collection class.
i would have to agree though, for scalablity ray's suggestion would be more favourable, but if this is not a concern, then you will find the tokenizer to be a breeze to code.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
In particular, we'd like display names to be of the format FIRST_NAME(S) + SPACE + LAST_NAME.
Thanks Pardner! Hope to see you 'round the Ranch!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Peter Read

Joined: Apr 21, 2004
Posts: 7
First of all Sorry about the name thing
The other thing,
Just so im getting this correctly

So this means every time i type tokenizer.nextToken(); it will refer to a different column in the file so if i type it 3 times it will go along to the 3rd coloumn and copy that information?
Max Habibi
town drunk
( and author)

Joined: Jun 27, 2002
Posts: 4118
Hi Peter,
I think I solved a very similiar problem towards the end(11th post?) of this thread. Swap out ":" for ",", and you should be good to go.
That is, replace line 27 with

All best,
[ April 21, 2004: Message edited by: Max Habibi ]

Java Regular Expressions
Have you checked out Aspose?
subject: Getting certain information out of a .csv file
It's not a secret anymore!