Solved

Using Enums with integer values in a Switch statement not working

Posted on 2014-01-19
4
198 Views
Last Modified: 2014-01-24
Hi, I need to use a set of enums containing integers for a switch statement. I am trying to then compare the enum values to a list of array values beeing passed into my method(the sitePosition of the array is the control value). I can't seem to figure out how to do this as everything I have applied, to my knowledge, can't be done to the switch statement. Am I missing something obvious here or am I going about this the wrong way? Any advice would be appreciated. Below is a snippet of my code. Thanks.

public enum Sites{
SITE_1(1),
SITE_2(2),
SITE_3(3);

Sites(int value) {this.value = value;}

private final int value;

public int value() {return value;}
}

//method to compare array value to enum

protected List<String> getLocationNames(Iterable<Locations> locations){

List<String> locationNames = Lists.newArrayList();
String siteName ="";



for(Locations location : locations){

  switch(location.site.sitePosition()){

    case Sites.RECEIPT_SITE_TRUCKED //doesn't work complains int required
             siteName = location.component().name() + " trucked receipts";
               break;

   case Sites.RECEIPT_SITE
             /// same code here as above but with different string appended

          break;
  //more case statements but left out for sake of brevity

}

   siteName = WordUtils.capitalizeFully(siteName);
   locationNames.add(siteName);
}
}

Open in new window

0
Comment
Question by:grnmachine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 39793015
Your code didn't show what type  location.site.sitePosition() returns.

It needs to be a Sites enum object - it looks like it might be instead returning an "int"?  If so you'd be comparing an int to an enum - and that's not allowed.

If you only have an "int" then you'd need to convert it to the enum value first, something like:

Sites val = Sites.values()[index] ;

or in your case since you're using Sites "1,2,3" you may want:

Sites val = Sites.values()[siteValue-1] ;

Also your enum only shows 3 values SITE_1 etc., but you're trying to match against other values that aren't in the enum (RECEIPT_SITE etc.)?

So overall you may want something like:

for (Location location....) {
   int siteValue = location.site.sitePosition() ;
   Sites val = Sites.values()[siteValue-1] ;
   switch (val) {
       case SITE_1:     // Note: No qualifier for Sites needed here if doing it right...
           ...
       case SITE_2:
   }
}

Hope some of that helps,

Doug
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39793537
I believe implicit enum / int conversion worked in C++ and maybe in early versions of Java but no longer, at least since Java 1.5.

Check out the ordinal() function of enum:
http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#ordinal%28%29
Note that it's strictly 0 .. (n-1), not the numbers of your choice.
There's an interesting solution with enum constructor here:
stackoverflow.com/questions/8157755/how-to-convert-enum-value-to-int
0
 
LVL 37

Expert Comment

by:zzynx
ID: 39793541
grnmachine,
In the code you posted we see that you have three enums:

Sites.SITE_1, Sites.SITE_2 and Sites.SITE_3

Open in new window


while in your switch code you use

Sites.RECEIPT_SITE_TRUCKED, Sites.RECEIPT_SITE, ...

Open in new window


Imo, you need to clarify that first.

And indeed, secondly tell us what the return type of
location.site.sitePosition()

Open in new window

is.
0
 

Author Closing Comment

by:grnmachine
ID: 39807483
excellent, thanks. I was able to apply your solution after cleaning up my code a bit.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jsp error 6 70
What is the use of Forwarding Class in java 1 52
activeMQ Queue Messages Enqueued and Messages Dequeued 1 107
Java import explained 4 48
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question