1

I'm coding a lottery program, and right now I'm kind of stuck. I let the user pick seven numbers, and at the end I want the program to tell the user which numbers he answered correctly.

I'm having so much trouble understanding arrays that I'm not sure how to store the correctly guessed numbers in an array and then print the elements in the array at the end. I've tried all sorts of variations but nothing is working for me.

package whatevs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

public class lottery {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    int[] userNumbers = new int[7];
    int[] winningNumbers = new int[7];
    int guesses;
    int i;
    int counter = 0;
    int[]correctGuessed=new int[8];
    int x;

    ArrayList<Integer> list = new ArrayList<Integer>();
        for (x=1; x<40; x++) {
            list.add(new Integer(x));
        }
        Collections.shuffle(list);
        for (x=0; x<7;x++) {
           winningNumbers[x] = list.get(x);
        }

    System.out.println("Pick 7 numbers between 1 and 39: ");
    for(i = 0; i < 7; i++){
        guesses = reader.nextInt();
        userNumbers[i] = guesses;
       // System.out.println(userNumbers[i]);
        for(x = 0; x<7;x++){
            if(winningNumbers[x] == userNumbers[i]){    
                correctGuessed[x] = userNumbers[i];
                counter+=1;
            }
    }

    if (counter == 7){
        System.out.println("You won!");
    }
    else
        System.out.println("You had " + counter + " numbers correct: " + correctGuessed[x]  );
    }
}
N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
virtusplow
  • 11
  • 4
  • 2
    Do you really want to use arrays to do that? Because a much easier and logical solution would be to use Sets. – JB Nizet Nov 26 '16 at 11:39
  • 1
    JB is right: http://stackoverflow.com/questions/18644579/getting-the-difference-between-two-sets – Simon Martinelli Nov 26 '16 at 11:41
  • I may know little about arrays, but I know absolutely nothing about sets. Would that mean I have to rewrite the entire program? – virtusplow Nov 26 '16 at 11:44
  • No, not the entire program. But you have only a few lines in that entire program anyway. Will write an answer to get you started. – JB Nizet Nov 26 '16 at 11:46
  • I don't think so, you can use your arrays and create a List of Integer and store the index of each similarities ! – Bo Halim Nov 26 '16 at 11:46

4 Answers4

1

I would use Sets instead of arrays. Sets have two advantages over arrays:

  • they ensure that there is no duplicate in the set (which is what you want, since you have distinct 40 numbers, and want the user to pick 7 distinct numbers)
  • they are much higher-level, and thus have many useful methods that bare arrays don't have

So here is the logic you should have:

  1. Fill a list with 40 numbers
  2. Shuffle it
  3. Create a HashSet<Integer> and add the first 7 elements of the list. Those are the winning numbers
  4. Create a new empty Hashset<Integer>
  5. Ask the user to enter numbers. Add each number to this new HashSet, and keep asking until the set has 7 numbers
  6. Use the retainAll() method of HashSet to know which of the guessed numbers are also part of the winning numbers. The javadoc is your friend to understand what this method does.
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Use list.contains(v) to check if wining values are among the guessed one. Something like this:

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    Integer[] userNumbers = new Integer[7];
    Integer[] winningNumbers = new Integer[7];
    int guesses;
    int i;
    int counter = 0;
    int[] correctGuessed = new int[8];
    int x;

    ArrayList<Integer> list = new ArrayList<Integer>();
    for (x = 1; x < 40; x++) {
        list.add(Integer.valueOf(x));
    }
    Collections.shuffle(list);
    for (x = 0; x < 7; x++) {
        winningNumbers[x] = list.get(x);
    }

    System.out.println("Pick 7 numbers between 1 and 39: ");
    for (i = 0; i < 7; i++) {
        guesses = reader.nextInt();
        userNumbers[i] = guesses;
    }

    List<Integer> winList = Arrays.asList(winningNumbers);
    List<Integer> guessList = Arrays.asList(userNumbers);
    List<Integer> matchList = new ArrayList<Integer>();
    for (Integer guess : guessList) {
        if (winList.contains(guess)) {
            matchList.add(guess);
        }

    }
    counter = matchList.size();

    if (counter == 7) {
        System.out.println("You won!");
    } else {
        System.out.println("You had " + counter + " numbers correct: " + Arrays.toString(matchList.toArray()));
    }
}// main
Alex Byrth
  • 1,328
  • 18
  • 23
0

Your code is a little complex but it is almost good to work.

Some remarks :

  • The object which contains the guessed numbers should not be a array of 7 elements : int[7] because otherwise it contains always 7 elements while the user may found less than 7 numbers. In this case, the other values will be to 0 (default value of int) and it you use Integer[7], it will be to NULL. It is probably not a suitable choice. You should rather declare :
    Set<Integer> correctGuessed = new HashSet<Integer>(); instead of int[]correctGuessed=new int[8];

  • When a number is guessed, you assign the found number in this way : correctGuessed[x] = userNumbers[i]; You could replace now by this : correctGuessed.add(userNumbers[i]);

  • At the end, you write in the output :

    `System.out.println("You had " + counter + " numbers correct: " + correctGuessed[x]  );`
    

It is not right for two reasons : - x has always as value 7 after the loop. So, it is not necessarily the value of the index of the array you have just filled. In your original solution, you should use a break if you want to keep the value of x when the number is guessed :

for(x = 0; x<7;x++){
            if(winningNumbers[x] == userNumbers[i]){    
                correctGuessed[x] = userNumbers[i];
                counter+=1;
                break;
            }
    }
  • you refer "to numbers correct" in the output. So, it seems more logical to display all guessed number :

    System.out.println("You had " + counter + " numbers correct: " + correctGuessed);

The final solution is very near from our one :

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    int[] userNumbers = new int[7];
    int[] winningNumbers = new int[7];
    int guesses;
    int i;
    int counter = 0;
    Set<Integer> correctGuessed = new HashSet<Integer>();
    int x;

    ArrayList<Integer> list = new ArrayList<Integer>();
    for (x = 1; x < 40; x++) {
        list.add(new Integer(x));
    }
    Collections.shuffle(list);

    for (x = 0; x < 7; x++) {
        winningNumbers[x] = list.get(x);
    }

    System.out.println("Pick 7 numbers between 1 and 39: ");
    for (i = 0; i < 7; i++) {
        guesses = reader.nextInt();
        userNumbers[i] = guesses;
        for (x = 0; x < 7; x++) {
          if (winningNumbers[x] == userNumbers[i]) {
            correctGuessed.add(userNumbers[i]);
            counter += 1;
          }
        }

        if (counter == 7) {
          System.out.println("You won!");
        }
        else
          System.out.println("You had " + counter + " numbers correct: " +  correctGuessed);
        }
      }
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

Your code quickly fixed looks like this:

    System.out.println("Pick 7 numbers between 1 and 39: ");
    for(i = 0; i < 7; i++) {
        guesses = reader.nextInt();
        userNumbers[i] = guesses;
        // System.out.println(userNumbers[i]);
        for (x = 0; x < 7; x++) {
            if (winningNumbers[x] == userNumbers[i]) {
                correctGuessed[counter] = userNumbers[i];
                counter += 1;
            }
        }
    }

        if (counter == 7){
            System.out.println("You won!");
        }
        else
            System.out.println("You had " + counter + " numbers correct: " + Arrays.toString(correctGuessed));

There is however a lot that can be improved, as other users already said. You don't want to use an array to store the winning values, you could use a Set to do that:

    Set<Integer> correctGuesses = new HashSet<>();

    for(i = 0; i < 7; i++) {
        guesses = reader.nextInt();
        userNumbers[i] = guesses;
        for (x = 0; x < 7; x++) {
            if (winningNumbers[x] == userNumbers[i]) {
                correctGuesses.add(userNumbers[i]);
            }
        }
    }

    if (correctGuesses.size() == 7){
            System.out.println("You won!");
    }
    else {
            System.out.println("You had " + correctGuesses.size() + " numbers correct: " + correctGuesses);
    }

I suggest not to use arrays at all, since they might be pretty complex to use. If you use Collection types, you have a lot of handy shorthand methods. Instead of looping over the array with winning numbers to see if the userNumber is in it, you can just use contains(userGuess). You also don't need to save everything. If you don't need to save the numbers the user has guessed, you could just not store them and only work with the current guess. For example:

System.out.println("Pick 7 numbers between 1 and 39: ");
    for(i = 0; i < 7; i++) {
        int currentGuess = reader.nextInt();
        if (winningNumbers.contains(currentGuess)) {
            correctGuesses.add(currentGuess);
        }
    }

But if you want to make sure that the user played distinct numbers in your lottery, you will want to save the users guesses. So you can use a Set again, and add the guess every time the user guesses. You could use a while loop to keep asking for a number until the user has given you 7 unique numbers. Something like this:

while (guessedNumbers.size() < 7){
        int currentGuess = reader.nextInt();
        guessedNumbers.add(currentGuess);
        if (winningNumbers.contains(currentGuess)) {
            correctGuesses.add(currentGuess);
        }
 }

This should give you enough material to go refactor the other code with all the hints of JB Nizet and Alex.

May I mention one extra thing: try to use methods and variables with intention revealing names. You are doing good already, but try to be as specific as possible, if the int contains 1 guess, call it guessedNumber and not guesses. Or if the user has won when the counter is 7, you could introduce a boolean won for example, or an extra method. I must say the code looks pretty good already, considering the fact that you are struggling with arrays.

Nick Vanderhoven
  • 3,018
  • 18
  • 27