This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes condensing code Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "condensing code" Watch "condensing code" New topic
Author

condensing code

Melinda Wilson
Ranch Hand

Joined: Jul 05, 2001
Posts: 35
Can any part of this code sample be furthered condensed?
**part one

**part two

Thank you for any suggestions
[ May 06, 2002: Message edited by: Dirk Schreckmann ]
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

... this looks an awful lot like a homework assignment ...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Melinda Wilson:
Can any part of this code sample be furthered condensed?

Yes. Basically you are having a mapping between Strings and ints.

Than you can easily use the Map to lookup ages for a specific String. Take a look at java.util.Map and its implementations.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Melinda Wilson
Ranch Hand

Joined: Jul 05, 2001
Posts: 35
Originally posted by Jessica Sant:
... this looks an awful lot like a homework assignment ...


No it is not a homework assignment.
[ May 07, 2002: Message edited by: Melinda Wilson ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
Part II has a lot of redundancy. It seems to reduce to

This could probably be accomplished as:

For part I, I'd probably list all the states in a file, and read the file into a Map of some sort which allowed me to look up the min age for a given state, without a bunch of if/else if clauses. The Properties class would be a simple way to do this. It's probably not worth the trouble for the code as it is now - but if you plan to add the remaining states, or if you need to have other state-dependant information, I'd try to put it in a separate file where it's easy to read and update if necessary.


"I'm not back." - Bill Harding, Twister
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
code:
--------------------------------------------------------------------------------
if (strState == "AR" || stState == "IA" || strState == "NE" ){ intMinAge = 14;}else if (strState == "IL" || stState == "CO" || strState == "KY"){ intMinAge = 15;}else if (strState == "MO" || stState == "IL" || strState == "TN" || strState == "KS"){ intMinAge = 16;}

Also, it is not a good idea to test the equivalence of Strings with the "==" operators. Use the str1.equals(str2) instead.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: condensing code
 
Similar Threads
SelectItem Help needed
dependent select list options
Throws clause throws compile error
help with validation/ 2 functions on submit.
ResultSets Lose Second half of getString