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

Enum in java 1.4

 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
i have a method that returns string value and i want to use this string in the switch case. I tried using enum but it is not available in JAVA 1.4. can anbody suggest something.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please ask such non SCJP specific questions in our Java In General (intermediate) forum. Moving...
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by deshdeep divakar:
Hi All,
i have a method that returns string value and i want to use this string in the switch case. I tried using enum but it is not available in JAVA 1.4. can anbody suggest something.


You can only use integer values in switch statements. So if you must use a switch statement, you'll need to write some code to map your String to an integer value, and test that. Alternatively just use an if statement.
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i Cant use if else as the ladder will be too long. n how do i map it to integer. Cant i use Enumeration...???
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i Cant use if else as the ladder will be too long

Will the switch statement not be of a simmilar length?


n how do i map it to integer.

Define a class which contains public static final integer values for each of the supported conditions and write method that takes a String and returns one of these integer values. You don;t need this though if you just use an if statement directly on the String value.


Cant i use Enumeration...???

No. An Enumeration is a legacy class for iterating through the values of a Vector or Hashtable. And enums are not available till JDK 1.5.
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is not switch statement in java for Strings. But i think you can solve it using 'Chain of responsability' pattern if you have a lot of case statements. If you have just 3 or 4 case statements use if-then-else with the String.equals evaluation.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how a Chain of Responsibility would fit here. But you could use a Command pattern, and put the commands in a map. Here's a quick example:
Depending on what sorts of things you need to do here, you may want to define execute() with some arguments, or give it a non-void return type.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic