• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

First Program- just wondering what ya'll think

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of the if, else if... you could do something like this:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic