aspose file tools*
The moose likes Beginning Java and the fly likes Confusion regarding bad habits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion regarding bad habits" Watch "Confusion regarding bad habits" New topic
Author

Confusion regarding bad habits

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
Guys,

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?

Thanks!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Hi Ernest Friedman,

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 ..


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion regarding bad habits
 
Similar Threads
StreamTokenizer usage
StreamTokenizer vs StringTokenizer
How to count no. of tokens?
Parsing a .txt file
reading passwords and connections from a flat file