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.
I've wrote a program which reads a file and created objects based upon the values on each line in the file.
Each line has numerous tokens which are related to each other (when on the same line - much like a database).
The program works great and i'm quite happy with it. But... I want to ensure i'm not limiting code at all and to ensure "correctness" e.g. using the java.* functions the way they are meant to be used.
Basically at the moment I create a BufferedReader round the file then read a line in and pass it to a StringTokenizer. Now because I know what tokens are placed where in the file I create objects using the StringTokenizer.NextToken command for each value I need.
Is this the right way to do things? (I know there isn't a right way as such but i'm learning as I go and want to ensure I don't develop bad habits) Should I use the StringTokenizer to evaluate whether each subsequent token is double/string etc?
From the general description, there's nothing wrong with what you're doing. If the data files were really huge, then using StringTokenizer -- not the world's most efficient class -- might be less desirable; the same is actually true of reading a line at a time with BufferedReader. But for moderate sized files, your process is very typical.
You can make yourself crazy trying to anticipate and handle various types of errors. What if a line had a missing token? Or a letter where it should have a number? Give some thought to what output you'd like to see in these cases and do the least possible "barely sufficient" code to get there. Checking and error handling code can quickly clutter up your program until it's hard to see what you're trying to do, so don't do any more than really necessary. Maybe the right answer is to do nothing - let the program halt with an exception. [ November 20, 2004: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Nice to see you here in this forum .. Then regarding the StringTokenizer , i have read some where , may be in this forum that it's better to avoid String Tokenizer and use some other way, can you throw some light on this ? What are the advantages & the disadvantages ..