• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Name of enum object in all lower-case

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modification to Sierra/Bates, Chapter 3, Question 7.

Please note line 14 where Suits.SPADES automatically prints the name of the enum.

Question 1: Is this an in-built function of the enum?

Question 2: How do I implement toString() such that is prints the name of the Suits object in all lower-case. I attempted this in line 9, but in vain.

 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you checked the API for java.lang.Enum for something that might help you?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just looked at it.

Even though it advises AGAINST overwriting toString(), I would like to know how to do it anyway (to study for the SCJP exam).

Would like it such that the name of the Suits object prints lower-case.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:Just looked at it.


And you saw nothing that might be helpful?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, where is your tolower() method?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Bear: I'm still looking at the API, trying to figure out anything that may help

@Abimaran: tolower() is on line 9
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandra, Abimaran is asking where have you declared tolower method in your enum. Its not a built in method for enums. There are 10 methods in java.lang.Enum class, one of them returns the name of the enum i.e. if we declare our enum as SPADE, that method will return "SPADE", you can convert it to lower case once you have the enum name as a String...
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:Modification to Sierra/Bates, Chapter 3, Question 7.

Please note line 14 where Suits.SPADES automatically prints the name of the enum.

Question 1: Is this an in-built function of the enum?

Question 2: How do I implement toString() such that is prints the name of the Suits object in all lower-case. I attempted this in line 9, but in vain.


AFAIK
1) Suits.SPADES is an Enum Constant.
An Enum constant is internally a a final static reference of the Enum Type Suits.
When Suits.SPADES is referred to in the function System.out.println, toString() function of this reference will be invoked.
Default toString() function of the Enum class invokes the name() function of the Enum class which returns the constant name as declared in the enum.

2) Replacing toString() function in this code to the code below would give you the desired result:
public String toString(){ return name().toLowerCase();}
This invokes the same name() function as mentioned above followed by toLowerCase() function of the String class.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Vicky: I updated the code and it now works.




@ All: Guess I need to practice UNDERSTANDING an API :-)


Thanks!!!
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"Practice makes perfect" which is why I sent you to the API instead of just blurting out the answer.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Sandra, Abimaran is asking where have you declared tolower method in your enum. Its not a built in method for enums. There are 10 methods in java.lang.Enum class, one of them returns the name of the enum i.e. if we declare our enum as SPADE, that method will return "SPADE", you can convert it to lower case once you have the enum name as a String...



Oh! Sorry, I misunderstood the question!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i have a question, it sure does seem like

System.out.println(Suits.SPADES + " " + Suits.SPADES.points);

shouldn't be accessible because it is private.

I suppose this is because enums are "special" and is part of the bridge class?

 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing "special"... it acts just like any other inner class.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Myname Is wrote:
shouldn't be accessible because it is private.

I suppose this is because enums are "special" and is part of the bridge class?


Try this...

 
John Avila
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right thanks.
Not sure why i didn't think of that.
 
Something about .... going for a swim. With this tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic