Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Nested if's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested if Watch "Nested if New topic
Author

Nested if's

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
I am supposed tochange this from a nested if to something else but I don't know what else to changed it to any help would be appricated.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You could implement it usning enums.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
i think he wanted me to get rid of the if's all together...doesn't emun's use ifs
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
You could use a HashMap with the timezones as keys and the minutes as Integer values.
[ March 31, 2006: Message edited by: Joanne Neal ]

Joanne
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
whats a hashmap
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Or if this is a real-life question and not just a beginner's programming exercise, you could replace the whole thing by calls to various methods in the TimeZone class. No "ifs" would be required there.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You dont have to use if's if you use enums. You could use the valueOf() method.

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
Well I am in intermidate programming but its the beginner stuff I don't understand.....I am having trouble with the basic concepts...thats why I post here
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The Map approach is something I use all the time. Look at the JavaDoc for all the details. Here's a method that might be similar to yours:

See if you can read up on HashMap enough to do somethin glike that.


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
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Sounds like the perfect fit for the Strategy Design Pattern. Some proponents of language theory proclaim that a single if/else is a broken construct and does not belong in a complete language. While I haven't bought that argument just yet, I certainly agree with the more wider (though still small relative to the critical mass) understanding that a if/else/else (3 or more statement bodies) should always be avoided. I even know a few who claim that for loops are flawed constructs!! I haven't bought that argument, simply because I don't understand it in completeness yet, but I suspect that they are ultimately right. For the same reason - appropriate abstraction/accurate requirement expression - switch/case constructs should always be avoided.

The point being, I suspect that your teacher (a knowledagable academic?) is probably trying to teach you something related to this and I encourage you to pursue it further.

The Strategy Design Pattern - one of few legitimate design patterns - is implemented in Java almost always using a java.util.Map. If you wish to go for further purity (but falling infinitely short implied by Java), you might want to consider net.tmorris.adt.map.Map - or just write the interface yourself. Minimalism is paramount even though it is often unachievable under a given context. So often I observe suffering as a result of excessive expression of requirement/contract, and worse, I observe the resulting "problem" attributed to a consequence, instead of the underlying flaws.

Good to hear of a tertiary institution on the right path ...if indeed that is what is happening.


Tony Morris
Java Q&A (FAQ, Trivia)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Tony Morris:
Good to hear of a tertiary institution on the right path ...if indeed that is what is happening.


For all we know the instructor is trying to push them towards a type-safe enum and a switch/case so I wouldn't get my hopes up too much.

As for the OP's question, I think using a Map is your best bet and someone already gave you sample code for that.
 
wood burning stoves
 
subject: Nested if's