8 Replies Latest reply: May 29, 2013 8:29 PM by EJP RSS

    Strings With Digits Not Permitted Enum Values ?

    KonTiki
      If you have an enumeration type with instances whose publicly accepted names are numbers,
      e.g. a Chevy 396 and I'm sorting this car according to makes and models that I've already laid out within enum types for each.
      Here the make instance is "Chevy" and the model instance is just "396".

      I know I could make this "396" into "Ch396" and this would work programmatically.
      But this might confuse some people who just expect to see "396".

      In short, why are enum instances not allowed to be of String type rather than just standard Java identifiers ?

      Edited by: KonTiki on 28-May-2013 05:34

      Edited by: KonTiki on 28-May-2013 05:35

      Edited by: KonTiki on 28-May-2013 05:37
        • 1. Re: Strings With Digits Not Permitted Enum Values ?
          gimbal2
          KonTiki wrote:
          In short, why are enum instances not allowed to be of String type rather than just standard Java identifiers ?
          What changes when you know? You still can't do it. I would rather focus on finding a proper design solution, I mean I'd assume you want to actually make some progress in stead of wondering why oh why :)

          You could add an actual String or int field to your enum, like so:
          public enum MyEnum {
            
            CH396(396);
          
            private int num;
           
            private MyEnum(int num){
              this.num = num;
            }
          
            public int getNumber(){
              return num;
            }
          }
          And then you can use the getNumber() method wherever you would like to display just the number.
          • 2. Re: Strings With Digits Not Permitted Enum Values ?
            KonTiki
            Yes. Solid point. But why not be curious ? (!)
            Maybe finding that Java language developers had a good reason for not doing it the way I'd like would make me have more confidence in them in future.

            Your code suggestion is an option for getting stuff OUT of an already set enum.
            My activity was more about checking user inputted model strings against the full set of existing make/model combinations already stored within enums. (I want to avoid data entry error.)

            Maybe enums are not really demanded here at all.
            I can program it to restrict users to imputting make/model/variant data from existing GUI drop-down lists of all possible makes/models/variants for each make of machine.
            This just requires me to use the same data arrays for makes, models and variants on the GUI as in the input processing methods and then I'm free of data inputting errors -- as long as those arrays are entered right.

            I also notice that using one enum as an element of another bigger one does not seem to work.
            Or maybe I'm not referencing things properly.

            Thanks.

            Edited by: KonTiki on 28-May-2013 09:07

            Edited by: KonTiki on 28-May-2013 09:08
            • 3. Re: Strings With Digits Not Permitted Enum Values ?
              rp0428
              >
              In short, why are enum instances not allowed to be of String type rather than just standard Java identifiers ?
              >
              That question doesn't make any sense.

              An enum 'instance' is of type 'enum'; how could it also be of type 'String'?

              The 'instance' you refer to is an 'enum constant' and, like any constant, uses a Java identifier as a name.

              Identifiers, by definition, per the Java Language Spec must begin with a Java letter.
              http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8
              >
              3.8. Identifiers
              An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
              >
              It wouldn't make any sense at all to allow the name of one language element to be defined differently than everything else in the language.
              • 4. Re: Strings With Digits Not Permitted Enum Values ?
                Kayaman
                KonTiki wrote:
                Maybe enums are not really demanded here at all.
                Definitely not. It borders on the idea of creating a class for each type of automobile, something which might not seem obviously wrong to people new to OOP, but which doesn't bode well for actual design.
                • 5. Re: Strings With Digits Not Permitted Enum Values ?
                  KonTiki
                  @rp0428 :

                  I beg your pardon.
                  I ought have written enum literal, not enum instance.

                  Okay, having literals as identifiers is the way Java (and Ada too) implement enumeration types.
                  Somehow I'd overread that. And I'm not surprised why.
                  If you read intros to enumeration types in textbooks, they nearly always 'sell' them as a way of handling simple data types with non-primitive data. The big advantage being that subsequent revisers of the code cannot do dangerous shortcuts with them as they would with primitive data types used to signify their true values. Ada for sure sells enumeration types as a software reliability/security feature.And reliability must also at least one reason for Java adopting enums too.
                  In most examples provided in the textbooks, e.g. days in a week, colours in a traffic light, ethnicity descriptors for staff in employment records, etc, we are dealing with sets of strings - the very same strings we use when referring to these variables' values in everyday life.
                  Hence my disappointment in finding that this equivalency evaporates when the strings are digits.

                  I'm not sure that having such a structure as I seek would be a bad thing in language.There must be many applications where one has a designated set of numbers corresponding to a set of items.
                  But perhaps enum type implementation in Java and other languages is designed with other considerations in mind.

                  @ Kayaman : Thank you for your response.
                  Can you be more specific ? Do you mean software-functionally dangerous (which I can't readily see) or just plain obtuse and inefficient (which I can see !) ?

                  I take it you recognize that any decent Car record must have such attributes as :
                  * make
                  * model
                  * variant
                  * bodyStyle
                  * engineSizeL
                  * engineType
                  * fuelType
                  * extraFeatures
                  * yearPurchased
                  * originalPrice

                  It seems to me that all attributes bar the last three would be candidates for enum typing, if it weren't that :
                  1. It is problematic for the 'model' attribute in this case where you have a different set of models per each make and the correct enum set must be selected on the basis of the current make value;
                  2. Model values ought be numbers but Java does not allow this; and
                  3. Alternative methods - in my particular case - are available to achieve the same code reliability with superior speed.

                  If an alternative method were not available to me, what operational dangers would you see me running into with enum types were deployed for the first 7 Car attributes ?
                  • 6. Re: Strings With Digits Not Permitted Enum Values ?
                    EJP
                    In most examples provided in the textbooks, e.g. days in a week, colours in a traffic light, ethnicity descriptors for staff in employment records, etc, we are dealing with sets of strings - the very same strings we use when referring to these variables' values in everyday life.
                    Days in a week are fixed. Colours in a traffic light are fixed.. 'Enthnicity descriptors' are a really poor example as they are subject to change. It's the kind of thing that belongs in a database lookup table, as does your own usage.
                    I'm not sure that having such a structure as I seek would be a bad thing in language.
                    It would be syntactically ambiguous, and therefore unimplementable. That's really the end of the discussion.
                    I take it you recognize that any decent Car record must have such attributes as :
                    * make
                    * model
                    * variant
                    * bodyStyle
                    * engineSizeL
                    * engineType
                    * fuelType
                    * extraFeatures
                    * yearPurchased
                    * originalPrice

                    It seems to me that all attributes bar the last three would be candidates for enum typing
                    None of them are. They are all attributes that should be in a database, along with their possible values.
                    If an alternative method were not available to me, what operational dangers would you see me running into with enum types were deployed for the first 7 Car attributes ?
                    You are hardwiring things intot the application that should be in the database. One new manufacturer, one new model, one new feature and you have to revise your code.
                    • 7. Re: Strings With Digits Not Permitted Enum Values ?
                      KonTiki
                      It would be syntactically ambiguous, and therefore unimplementable.

                      An enum with String literals would be, of course.
                      But I meant some other structure. But I suppose these already exist in a way with Sets and all that.

                      You're bang on with the rest of it.
                      Making the Car class attributes either String, int or double and controlling both user-input data and associated Car data required by the program from the one database is the easy way to go here.
                      No doubt the database will have be enhanced/expanded in both attributes and allowed values as the program is refined and this will lead to changes in Car class too.
                      But giving primacy to the database - Car class axis willl 'organize' these modifications to the program.

                      I think we'll leave it at that.
                      Thanks for all your help, as usual.
                      • 8. Re: Strings With Digits Not Permitted Enum Values ?
                        EJP
                        KonTiki wrote:
                        It would be syntactically ambiguous, and therefore unimplementable.

                        An enum with String literals would be, of course.
                        An enum with numerical names would be, of course, which is what the thread is about.