Reputation: 45
So I'm extremely rusty with my java programming. I'm trying to create a hypothetical company lottery where each employee can only enter once. Then a name will be randomly generated announcing the winner. I'm not even sure I have some things in the right order at the moment. Any help on this will be greatly appreciated. I looked up some things and I believe it to have made it worse.
// instance variables
final int NUM_PARTIC = 7; // number of workers participating
String input; // holds each name
int i, j;
// Create array to hold number of particpants.
String[] nameArray = new String[NUM_PARTIC];
// Create a Random class object.
Random stakes = new Random();
for(i = 0; i < nameArray.length; i++)
{
// Prompt participant for name.
input = JOptionPane.showInputDialog(null, "Please enter your"
+ " name into our database:", "Entry", JOptionPane.QUESTION_MESSAGE);
nameArray[i] = input; // Store name in namesArray[]
// Prompt next participant for name
input = JOptionPane.showInputDialog(null, "Please enter your name into our"
+ " database:", "Entry", JOptionPane.QUESTION_MESSAGE);
nameArray[i] = input; // Store name in namesArray[]
for(j = i + 1; j < nameArray.length; ++j)
{
JOptionPane.showMessageDialog(null, "Sorry, this name is already "
+ "in our database", "Invalid", JOptionPane.ERROR_MESSAGE);
input = JOptionPane.showInputDialog(null, "Please enter your name into our"
+ " database:", "Entry", JOptionPane.QUESTION_MESSAGE);
}
// Display winner
JOptionPane.showMessageDialog(null, "The winner for today's raffle "
+ "is: " + nameArray[i], "Winner",
JOptionPane.WARNING_MESSAGE);
// Exit program
System.exit(0);
Upvotes: 0
Views: 137
Reputation: 772
Using random to select a random value from given values
String randomLotteryWinner()
{
String []array={"person1","person2","person3","person4","person5"};
String Winner=array[(int)(Math.random()*array.length)];
return Winner;
}
Upvotes: 0
Reputation: 7507
All java collections have the contains
method which searches that collection for the given item. This would be much preferable to searching an array.
In this case the best would be the Java collection Set
which allows O(1) (aka very fast searches) for the same element, or just use a List
if you want to be able to retrieve arbitrary elements like the array. Either way the method to find an element is done for you.
If you have to do this inside of an Array. You will need to iterate over the array.
public static boolean contains(String[] array, String search){
for (String s: array){
if (s.equals(search))
return true;
}
return false;
}
Upvotes: 0
Reputation: 22241
What do you think you're doing in the code below? It's certainly not searching previous entries. Please speak to yourself and say what you are doing, then say what you want to do, then compare these two answers.
for(j = i + 1; j < nameArray.length; ++j)
{
JOptionPane.showMessageDialog(null, "Sorry, this name is already "
+ "in our database", "Invalid", JOptionPane.ERROR_MESSAGE);
input = JOptionPane.showInputDialog(null, "Please enter your name into our"
+ " database:", "Entry", JOptionPane.QUESTION_MESSAGE);
}
Besides: just use a List<String>
instead of String[]
.
To get a random winner just use Random
class with it nextInt(nameArray.length)
method.
Upvotes: 1
Reputation: 4798
Use java.util.Random
class for randomly selecting a winner from nameArray
Random rand=new Random()
int winnerIndex=rand.nextInt(nameArray.length);
then
JOptionPane.showMessageDialog(null, "The winner for today's raffle "
+ "is: " + nameArray[winnerIndex], "Winner",
JOptionPane.WARNING_MESSAGE);
Upvotes: 0