*
The moose likes Beginning Java and the fly likes First Program- just wondering what ya'll think Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "First Program- just wondering what ya Watch "First Program- just wondering what ya New topic
Author

First Program- just wondering what ya'll think

Joseph Clifton
Greenhorn

Joined: May 22, 2005
Posts: 2
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 ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

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!


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

Joined: Jan 29, 2003
Posts: 8791
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
Joseph Clifton
Greenhorn

Joined: May 22, 2005
Posts: 2
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.
Charlie Goth
Ranch Hand

Joined: Feb 26, 2004
Posts: 60
Originally posted by Joseph Clifton:
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.





You might want to wrap the whole code in a infinite loop too, allowing the user to translate again without restarting the program.


SCJP (77%)
Antony Larant
Greenhorn

Joined: May 24, 2005
Posts: 1
Also you should avoid += and use StringBuffer. Only use += for very simple cases.

To continue learning, why not write a class called Leet that provides a method called convertTo1337 have main deal with the IO and Leet handles the computations.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Instead of the if, else if... you could do something like this:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: First Program- just wondering what ya'll think
 
Similar Threads
determining an input's data type.(part 2)
Files vs Arrays?
If only one vowel is entered in the string, then that vowel should be diplayed on console
Help with an encoding/decoding program
PrintStream problems