0

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);
3
  • 2
    You'll need to ask a concrete question before anyone can help you. What is currently going wrong, or what part do you not know how to do? Commented Jun 4, 2013 at 20:02
  • 1
    Why you using Array? use Set as it won't accept duplicates. And it is easy to iterate over them. Commented Jun 4, 2013 at 20:06
  • Hint: Write some functions. Commented Jun 4, 2013 at 20:08

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll try these out. Totally forgot about using a list for this. It makes perfect sense. Thank you people. I am basically starting programming as I have only done java 1 and 2 about a year ago. This helps.
0

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);

Comments

0

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;
}

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.