JAVA ENUM Random?
-
Why wont this code work? I need a way to randomly select values out of an enum. "enum e{One, Two, Three, Four, Five}; Random r = new Random(); e myEnum = r.nextInt(4);"
-
Answer:
The first value in the enum will be nought. 'e' is not made a type (like in pascal) so your third line will not work unless you make a class 'e'. nextInt(int) returns an int. try this - public class myEnumTest { enum e{Nought, One, Two, Three, Four, Five}; public static void main (String args[]) { Random r = new Random(); int iran = r.nextInt(6); System.out.println (Integer.toString(iran)); System.out.println (e.values()[iran]); } }
chase at Yahoo! Answers Visit the source
Related Q & A:
- Is Guillou-Quisquater existentially unforgeable against adaptive message attack under a random oracle model?Best solution by Cryptography
- How To Calculate Approximate Expectation Of Function Of A Binomial Random Variable?Best solution by Mathematics
- Why are random letters inserted in my tmux session?Best solution by Super User
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- Difference between Java 2 and Java 6?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.