• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion regarding bad habits

 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic