?
Solved

Using Enums with integer values in a Switch statement not working

Posted on 2014-01-19
4
Medium Priority
?
200 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 28

Accepted Solution

by:
dpearson earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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