File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes help req. regarding parsing a file and tokenising the contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help req. regarding parsing a file and tokenising the contents" Watch "help req. regarding parsing a file and tokenising the contents" New topic
Author

help req. regarding parsing a file and tokenising the contents

Paul Andrew
Greenhorn

Joined: Mar 24, 2006
Posts: 4
Hello.
Im fairly new to java, and need help to complete a task.

I need to read in a text file, and each line on it for errors and print out messages accordingly.

So far, reading the file is fine, but I need to split the lines into tokens. The lines are formatted as follows

surname:forename:1234:01-01-06

Im having trouble creating the tokens(split by : ) . Ive tried using stringtokenizer but to no avail. Are there any other ways of splitting the strings? (However, I cant use the regex items.)

Once tokenized, I intend to check the tokens like this...


Sorry if i should have put this in another section of the forum..

Many thanks
[ March 24, 2006: Message edited by: Paul Andrew ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
If you're using Java 1.5 you can use String.split();

[ March 24, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30913
    
158

Paul,
Welcome to JavaRanch!

StringTokenizer should work. Note that you need to pass ":" as the token. Passing ": " looks for colon or space.

You can call
String token = token.trim() on the token to get rid of the whitespace afterwards.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Garrett Rowe:
If you're using Java 1.5 you can use String.split();


[ March 24, 2006: Message edited by: Garrett Rowe ]


Or if using older java, the same method is available in Jakarta Commons Lang StringUtils


ASCII silly question, Get a silly ANSI.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help req. regarding parsing a file and tokenising the contents