wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Else if.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Else if.." Watch "Else if.." New topic
Author

Else if..

Ryan Waggoner
Ranch Hand

Joined: Jun 27, 2007
Posts: 75
Hey guys,
I just needed to come up with an alternate to creating a conditional that ends up having 20 else if statements.

If it helps, I am just receiving an object and looking at a particular property and then redirecting to a method based on the value of that property.

Any ideas on a better, cleaner way to be doing this?
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

can you not use a Select Case statment, it may be called something else in Java.


Visit my blog! http://jameshambrick.com
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

It's called "switch" in Java, and that's definitely an option.


[Jess in Action][AskingGoodQuestions]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

When you have this type of thing, consider whether polymorphism and overriding is a better solution.

But there is a place for the switch statement. However, switch only works on certain types (byte, short, int, char, and enum).

If neither polymorphism or switch work for your domain, then the if-else-if-else-if... is what's left and it has its place too.

Kaydell
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The other main option is to use a Map (usually a HashMap) to take a String or other object type and use it to look up some value. There are different ways to do this, depending on what you're trying to do within each if block.


"I'm not back." - Bill Harding, Twister
Ryan Waggoner
Ranch Hand

Joined: Jun 27, 2007
Posts: 75
I was also hoping to avoid a switch statement that had 20 or so cases.

With a hashmap, can I look up a value, and have that value be associated with a method?

So search for a value, find it, and then call the associated method?


I guess I am just looking for a more elegant, or cleaner way of making code when dealing with a huge conditional statement, whether it be switches, or else ifs...

Can I do what I am trying to?

Does it make sense?
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

I'm not sure I understand exactly what you're trying to do.

Can you have one base class with 20 subclasses and have an object reference variable of the base class and call its method. Then the right things would just happen automatically, but I'm not sure if this would work for what you're doing.

Kaydell
Ryan Waggoner
Ranch Hand

Joined: Jun 27, 2007
Posts: 75
That does sound a little better...

Is there something I can read on doing that sort of thing, I am not quite sure how to start doing that...
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

The keyword to search for is "polymorphism".

You can try my "Java Search" below and search "Java Books" for "polymorphism".

Kaydell

Java Search
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
With a hashmap, can I look up a value, and have that value be associated with a method?


Absolutely! What if you moved the code you want to run for each choice into the objects in the map?

Strategy is the name of one of the Gang of Four "design patterns", a common solution to a common problem. Can you imagine how the map could be full of strategies that know how to handle different scenarios? I made my strategies very simple here. You might have to pass in some parameters or call a couple methods on one if your problem is more complex.


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
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Stan James
The other main option is to use a Map (usually a HashMap) to take a String or other object type and use it to look up some value.


That's an interesting thought. I'll have to try Map objects.

Kaydell
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
I am talking WAYYYYY out of my league here..... I am only about halfway through with HFJ on my way towards my SCJP. BUT.... I have a 20+ year background in programming. What I am getting at, I don't know if this can be done in Java or not, but I thought I would throw the concept out there and let it get supported... or shot down.

From my C background I know that you can create arrays of pointers to functions and use an integer as a "function selector" or "handler selector" and simply reference the array using the "selector integer" to index into the array, pass any arguments in parens, and kaBAM you are gone in a couple of lines of code.

What I was thinking of here is an enum of values to serve as "handler selectors" and perhaps create a class Handler that had a reference to a method() as a variable, a constructor that allowed you to create a handler object, passing the method name to it, and in the class have a common method name that calls the stored method.
You could then declare an array of Handler objects and initialize it with the number of handlers you wish to define, passing each constructor the name of the handler for that enumerated value.

Now, you probably all think I'm out of my head over here because frankly I don't know if you could pull this off in Java or not. I have heard of something called delegates that allows the registration of methods() for later event handling, so I thought that there might be a way to use something like that to populate an array. If you could, talk about quick method dispatching......pass an enum into the main handler method and that enum is used as an index into an array of objects, passing any args required, and boom she was gone!

Well...... now that I have throughly embarrassed myself, I'm going to sit back and see what happens.....

Bob


------------------------
Bob
SCJP - 86% - June 11, 2009
Ryan Waggoner
Ranch Hand

Joined: Jun 27, 2007
Posts: 75
Stan,
I really like what your code is looking like, I am trying to look some things up on this, and still looking. I hope to be there soon...

Bob,
I dont have nearly as much experience as you do (only about 2 years), but I was wondering how do you go about creating those pointers?
Ryan Waggoner
Ranch Hand

Joined: Jun 27, 2007
Posts: 75
Two things I came across are reflection, and interface.

The interface (I think) has me put in an object into the hashmap that implements the interface.

The other thing that I was was reflection, which looks like I put the method name into the hashmap, and then look for in in some metadata(not sure where it comes from)

Both of these seem out of my league. I cant seem to find much information about these two techniques that I can understand, can anyone help dumb it down for me a little bit?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The interface solution seems interesting. See if you can follow what going on here:



[ July 13, 2007: Message edited by: Garrett Rowe ]
[ July 13, 2007: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Bob, we don't have pointers to functions in Java. Do you find the Strategies Garrett sketched out solve similar problems?

Ryan, we can get more abstract with reflection for method names. I'm feeling I'd rather not go there in the beginner forum, though. Too many choices, maybe.

I have a bunch of situations where I get all this from configuration. An application assembler reads config, sets up the map and hands it to the component that needs it.

Yes, that is dipping our toes in reflection, just not going over our heads.
Ryan Waggoner
Ranch Hand

Joined: Jun 27, 2007
Posts: 75
Ok great, thanks a lot. I am going to take that code sample and see if I can come up with something.

Be back soon
Ryan Waggoner
Ranch Hand

Joined: Jun 27, 2007
Posts: 75
Worked wonderfully, thanks for all of the help and code examples
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Else if..
 
Similar Threads
Struts Validation problem
c:when test = .....problem
struts1 and ajax - how to resume with struts after the 'ajax' is complete?
java.awt.headless - based on what is this System property set?
java.util.logging configuration