0

I am self-learning Java and am stuck on a simple project. I'd like to receive 6 unique 'lottery' numbers from a user.

  1. User will be asked to input an integer.
  2. Each user input will be placed into an array.
  3. If the user inputs a previously input number, I want to prompt to reenter the number again.
  4. Recheck the new input. If unique, continue the for loop. If non-unique, run step 3 again.

So far, all I have is:

public static int[] userLottoInput()
{
    int[] userNums = new int[6];        
    Scanner keyboard = new Scanner(System.in);

    for (int i = 0; i < userNums.length; i++ ) {
        System.out.printf("Enter Lottery number %d: ", i + 1);
        userNums[i] = keyboard.nextInt();

        for (int k=i; k<userNums.length; k++)  {
            while (k!=i && userNums[k] == userNums[i])  {
                System.out.printf("if");
                System.out.printf("Error! Try again: ");
                userNums[i] = keyboard.nextInt();
            }
        }
    }
}   

Any help is appreciated!!

2
  • 1
    I would change the structure of your logic so that you only ever ask the user for input in a single place... Commented Aug 27, 2014 at 4:57
  • It's kinda hard to ask what you're asking. SO isn't a place for us to do your code. Do you have a specific problem you need help with? Commented Aug 27, 2014 at 5:00

3 Answers 3

3

Try and keep you logic simple.

  • While the user hasn't enter 6 numbers, loop
  • Ask the user for a value
  • Check to see if it's a duplicate
  • If it is, ask the user to re-enter the value
  • If it's not (a duplicate) increment the counter to the next element...

For example...

public static int[] userLottoInput() {
    int[] userNums = new int[6];
    Scanner keyboard = new Scanner(System.in);

    int i = 0;
    // Keep looping until we fill the array, but
    // allow the control to fall somewhere else
    while (i < userNums.length) {

        System.out.printf("Enter Lottery number %d: ", i + 1);
        userNums[i] = keyboard.nextInt();

        // Check for duplicates
        boolean duplicate = false;
        // We only need to check up to i - 1, as all the
        // other values are defaulted to 0
        // We also don't need to check for the last number entered ;)
        for (int k = 0; k < i; k++) {
            // Check for duplicated
            if (userNums[k] == userNums[i]) {
                System.out.println("No duplicates allowed, please try again");
                duplicate = true;
                // Break out of the loop as we don't need to check any more..
                break;
            }
        }

        // If no duplicates where found, update i to the next position
        if (!duplicate) {
            i++;
        }            
    }
    return userNums;
}

With this, there is only one point at which you prompt the user. Everything else is used to control the element position (i) to meet your requirements.

Now, I'm sure that there are other ways to do this and this is just a simple example ;)

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

Comments

0

Move the asking of number outside loop, when received the number loop over the numbers array to find a match. If match found re-ask for number (outside the for loop used for finding the match), else if match not found, then add the number to array.

Comments

0

Don't you think your for loop is little complicated. Anyways, you can try this :

for (int k=0; k<i-1; k++)  {    //Start k=0 means from the first stored value
        while (k!=i && userNums[k] == userNums[i])  {
            System.out.printf("if");
            System.out.printf("Error! Try again: ");
            userNums[i] = keyboard.nextInt();
        }
    }

1 Comment

Yeah, no doubt in that. 1 iteration will be saved.

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.