Please use the CODE button; I have edited that post so you can see how much better it looks.
Please don't simply give out code like that. Since it is pretty standard code, which could have been copied from the Java Tutorials, I think I shall let it stand. But (look at the Beginners' Forum contents page), where we explain that people learn a lot better if they work out things for themselves.
It doesn't actually work in its present condition, and I can see a potentially serious error, which I shall let you find for yourself . I shall also leave you to work out what people would do in Java5 or Java6.
Yes, you can put those entries into a single List<String>, but is that really appropriate? I suggest you go through the different interfaces in the Collections Framework and you might find something more appropriate for keeping colours and numbers.
Joined: Dec 24, 2008
I have few things not quite understand from the code, what does line 21 and 34 actually doing??, because I have not cover WInputStreamReader and Iterator yet.
Also almost every codes thesedays has Try and Catch in them... are those required? does it prevent the program from crashing or halt when there is an error?
Joined: Mar 11, 2009
Thanks for letting me know to use Code Button. What error you are seeing in present condition please explain and user has asked about read and write data in file and he is reading data from existing file why you are giving him suggestion out of box.
Gary Lai wrote:Also almost every codes thesedays has Try and Catch in them... are those required? does it prevent the program from crashing or halt when there is an error?
If the API throws any kind of exception that inherets from java.lang.Exception the compiler will force you to surround the code with a try/catch block. This allows you to catch any exceptions that are thrown and deal with them. Some API's throw RuntimeExceptions which don't require try/catch blocks but if they throw an exception, the application will just die.
You are using the wrong classes for reading; you ought to use FileReader and BufferedReader because it is a text file. DataInputStreams are not designed for text files.
You are opening several Readers; I may be mistaken, but are you actually closing them? If you leave the Reader open, you may suffer a memory leak. That was what worried me. Anyway, when I tried your code, I couldn't get it to work; I got what appears to be a FileNotFoundException.
I would simply use the Scanner and Formatter classes for text files; they are much easier to use. Since they "consume" their Exceptions, you can get away without the try-catch.