Solved

Using Enums with integer values in a Switch statement not working

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now