• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How do I retrieve the value in this case ?  RSS feed

 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi expert,

I am facing problem in understanding how to retrieve the value from Enum.

With reference to below URL,

https://stackoverflow.com/questions/604424/lookup-java-enum-by-string-value (However, this may not also be the one that address my problem.

For example, in this Enum here :



My problem is that I need to compare a List<Object> against the Enum Key as shown above.

If the List<Object> contains the Enum Key, then it will give me the corresponding values.

To be more specific :



I have tried and search for a solution but seems like nobody is comparing the arrays or list of another class with Enum to get the values.

Really hope some can guide me in this area.

Tks.

 
Ranch Hand
Posts: 88
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the following thread
 
Saloon Keeper
Posts: 4758
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:I have tried and search for a solution but seems like nobody is comparing the arrays or list of another class with Enum to get the values.

Your post is not very clear about what you are trying to achieve. Maybe enums will be useful to you, maybe not. Rather than delving into the how's it would help if you described your use case, i.e. what is the real scenario you are trying to solve. Then maybe we can help you better. As it stands you've presented code snippets that don't make sense.
 
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the value of the enum by using something like the below.  Will this help you?
 
Carey Brown
Saloon Keeper
Posts: 4758
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Building on Knute's example. This code allows you to look up an enum using a String like "text2". There's some simple stream stuff here that could easily be coded as loops. It also makes use of the Optional class.
 
tangara goh
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joseph Mokenela wrote:Have a look at the following thread



Hi Joseph,

I have tried out as per the thread you have suggested.

However, I am not very clear how to do the Constructor of the class Subject if I have to make use of the Enum Keys Values instead of String.



Please let me know how do do the constructor for this part.





Tks.

 
Carey Brown
Saloon Keeper
Posts: 4758
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:


"array" is a List<Subject>, what does it mean for the list to "contain" a Blah.code? A Blah.code is not a Subject. The only thing that works with the "contains()" method is to search the list for an instance of another Subject.
 
tangara goh
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

tangara goh wrote:


"array" is a List<Subject>, what does it mean for the list to "contain" a Blah.code? A Blah.code is not a Subject. The only thing that works with the "contains()" method is to search the list for an instance of another Subject.



Hi Carey,

Thanks for your reply.

This is the part that I am facing problem.

Now I am not sure where to place the Comparable interface in my code ?  Should I put it in Subject or ?

I have to revise all these topics but I hope you can give me some tips how to go about doing the comparison - the elements in the array against the Enum Blah to give me the values if the two matches.

Tks.
 
Carey Brown
Saloon Keeper
Posts: 4758
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't posted the code for Subject yet. I assume it has one or more Blah's. If it has a List<Blah> then are you looking to see if the Subject contains any Blah, a particular Blah, or a matching list of Blah's?
 
tangara goh
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You haven't posted the code for Subject yet. I assume it has one or more Blah's. If it has a List<Blah> then are you looking to see if the Subject contains any Blah, a particular Blah, or a matching list of Blah's?



Here is my Subject class :

 
Carey Brown
Saloon Keeper
Posts: 4758
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:

Carey Brown wrote:You haven't posted the code for Subject yet. I assume it has one or more Blah's. If it has a List<Blah> then are you looking to see if the Subject contains any Blah, a particular Blah, or a matching list of Blah's?



Here is my Subject class :

Your "Subject" only has the attribute "name". Presumably your List<Subject> sublist is static (so that there's only one), in which case it should be initialized. Your Subject does not contain a Blah or a List<Blah>. As it stands now all you can do is search sublist for a particular subject by name. It's unclear from your prior posts where Blah fits into the picture at all. Can you describe in English what you are attempting to search for?
 
tangara goh
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:You can get the value of the enum by using something like the below.  Will this help you?



Hi Knute,

Tks for your example.

However, I have problem in adding the value which in my case is an int into a List<Integer>

Here's how I used your code to apply here :



Hope that you can tell me where have I gone wrong in the above code.

Million thanks.
 
Carey Brown
Saloon Keeper
Posts: 4758
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would really help us to help you. One way would be to provide properly formatted code. If you show effort then we are more likely to contribute. It would also help if you provided a stand-alone, subset of your project that we can compile and run (this would probably benefit you as well).

Here's an example, though it doesn't entirely compile. There are some missing pieces that I can't guess at.

Notice you have "i++" twice.
 
Carey Brown
Saloon Keeper
Posts: 4758
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"subjects" and "subjectlist" aren't being used.
 
tangara goh
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:It would really help us to help you. One way would be to provide properly formatted code. If you show effort then we are more likely to contribute. It would also help if you provided a stand-alone, subset of your project that we can compile and run (this would probably benefit you as well).

Here's an example, though it doesn't entirely compile. There are some missing pieces that I can't guess at.

Notice you have "i++" twice.







I hope you could let me know how to get the int value out.

Tks.
 
Knute Snortum
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is still not helpful, I'm afraid.

* Code doesn't compile
* Code not formatted correctly
* Many unnecessary lines of code
* Not clear what the code's intent is.
 
Knute Snortum
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to assume that your question is, How do I retrieve an enum values from a String value?  If that is correct, then try this:
 
tangara goh
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:I'm going to assume that your question is, How do I retrieve an enum values from a String value?  If that is correct, then try this:



Thank you so much.

You made it so easy where as I have been struggling this for SO L....O ....N....G ......Think I am not cut out to do coding....sigh....hope i can just finish this project will do.

Now, I am on to the next problem....


Thank you once again.....I am so thankful for your help.
 
Carey Brown
Saloon Keeper
Posts: 4758
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you are incrementing "i" twice.
 
tangara goh
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Note that you are incrementing "i" twice.



Thanks Carey.

I feel so alone struggling with my code.  Forums are the only place where I can ask for help.

I wonder if other people are also doing the same like me or they can just google and understand how to do things immediately?

 
Knute Snortum
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't feel alone.  This entire forum is dedicated to people like you learning and struggling with Java.  You should try googling first, but many times that is not enough.

If you're interested in really getting the most out of this forum, look through the wiki about how to ask questions.
 
Marshal
Posts: 60130
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:. . . I wonder if other people are also doing the same like me or they can just google and understand how to do things immediately?

As Knute says, everybody else does that sort of thing too. Beware of simply searching for guidance; you never can tell whether you are getting anything good or bad.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!