Solved

Using Enums with integer values in a Switch statement not working

Posted on 2014-01-19
4
197 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 32
Java: The Public Class Main 4 34
java mysql insert application 14 29
Eclipse for Java EE development 2 28
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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

820 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