Solved

Using Enums with integer values in a Switch statement not working

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 51
reverse digits of a number using for loop 5 44
How to execute a Python program and gather return output in Java 2 31
sql import cannot be resolved jsp 3 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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:

803 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