Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help req. regarding parsing a file and tokenising the contents

 
Paul Andrew
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Java 1.5 you can use String.split();

[ March 24, 2006: Message edited by: Garrett Rowe ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic