Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

First Program- just wondering what ya'll think

 
Joseph Clifton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joseph Clifton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Antony Larant
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of the if, else if... you could do something like this:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic