• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Enum in java 1.4

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please ask such non SCJP specific questions in our Java In General (intermediate) forum. Moving...
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic