N/APosted on - 04/13/2012
I really need some Java Programmers to help me solve this problem. I have a database which has already data stored in it. I'm planning to get a random value in the database every time it calls it. My program is a guessing game like Hangaroo and random words from db should be get.
For example in my database I have 5 values in it and then when the program opens, it gets a random one from those 5 values. I only know how to get values from a db but not randomly.
What will I do to make my program get a random value?
Calling a Random Value from a Database in JAVA
The things you will be needed are a variable that generates a random number and a java switch case.
Step 1: Import the class that generates random numbers in various ranges.
Step 2: Initialize a variable that receives a random number.
Random randomNumber = new Random();
int x = randomNumber.nextInt(5);
Variable x is needed/important in the next step to come up with a random value.
The number inside "nextInt(5)" which is 5 is the limit of the number to be generated. In the Example show the number to be generated is until 5. So the generated number will be 0-5.
Step 3: Use the Java Switch Case.
value = value1; // value in the database
value = value2; // value in the database
value = value3; // value in the database
value = value4; // value in the database
value = value5; // value in the database
The variable x inside "switch (x)" is now random.
Hope my answer help you.