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.
heya, 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 so tokenizer.nextToken(); 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. http://java.sun.com/j2se/1.3/docs/api/java/util/StringTokenizer.html -twans [ April 21, 2004: Message edited by: Antoine Waugh ] [ edited to break long line -ds ] [ April 21, 2004: Message edited by: Dirk Schreckmann ]
'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.
Joined: Apr 21, 2004
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 example,example1,example2,example3,example4 example,example1,example2,example3,example4 example,example1,example2,example3,example4 example,example1,example2,example3,example4 example,example1,example2,example3,example4 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
Joined: Jan 24, 2004
>>atm ive got the program to read through and store all this information however i only need the information contained in example3 dave, 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. -twans
Daveangel, 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!