Hey guys, pretty cool forum you guys seem to have here, I'll probably be sticking around.
Anyways, I'm finishing up a year-long course in Java at my high school and I've learned almost nothing. What I have learned, I've had to teach myself. Anyways, here's a very simple program I just wrote that translates text into "1337-speak" (yes I think this is very stupid but one of my friends said I couldn't do it so I did it). My style isn't very normal and ya'll probably won't like that, but please take a look and give me pointers as I'm looking to learn as much as I can about Java.
[ May 22, 2005: Message edited by: Joseph Clifton ]
edit: the formatting seemed to get further messed up when I copied and pasted it. Sorry not sure wha tthe best way to do this is.
[ May 22, 2005: Message edited by: Joseph Clifton ] [ May 22, 2005: Message edited by: Ernest Friedman-Hill ]
If you use the CODE tags, instead of the QUOTE tags, the formatting in your block of text will be preserved; I fixed this post for you as an example.
Good job on your first program! If you're interested in ways to make it better, here are my thoughts: note how every single branch of the "if" statement does the same thing. It's always bad to have duplicated code, for two reasons: one, it's easy to make a mistake in one of the copies, but hard to find it; and second, it's hard to change many copies of the same code -- harder then it would be to change just one copy would be, anyway.
So how do we have only one copy? Somehow, you'd want to have a lookup table which told the program the equivalent for each character. Then you could just do one lookup, and one catenation, for each character. There are a number of ways you might do this, some easier to describe, some more efficient, some easier to understand... The java.util.HashMap class would figure prominently in several of these ways. Rather than tell you what to do, exactly, I'll let you have a look at the documentation for that class and see what you can come up with on your own. Come on back and show us in this thread -- or ask a question here if you get stuck!
It takes a while to get used to exceptions and understand how you'd like to handle them. At the beginning like this you can just let em fly. If you add "throws Exception" to your main() declaration and remove the try-catch, then any exception will cause a useful display of just what went wrong. Another day, for users other than yourself, you might want to catch the exception and make a friendlier message.
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
Joined: May 22, 2005
I'll work on the looup table in a bit.
As for the exceptions thing, I just want to program to quit when the user clicks "cancel", I don't want a message explaining the exception. That's the reason why I did the catch statement.