Stephen Cunningham
Stephen Cunningham

Reputation: 109

Comparing 2 sets of integer values in 2 separate arrays

I am quite new to java and have this problem that I am struggling to work with. I have 2 sets of numbers, stored in two separate arrays, representing lottery numbers. The first set is the user numbers and the second set is the numbers from the lottery webpage. I have tried to compare the numbers position by position in the array but I am unsure as to what result leaves me with the correct number of matches and how I can include a match with the bonus ball, as there are 6 user numbers for the lottery, but 7 lottery numbers in the draw (6 numbers plus a bonus number). I have included my code below:

     // set up an array to store numbers from the latest draw on the lottery web page
     Integer [] numbers = new Integer [split.length];

     int i = 0;
     for (String strNo : split) {
        numbers [i] = Integer.valueOf(strNo);
        i++;
     }

     for (Integer no : numbers) {
        System.out.println(no);
     }

     Element bonusElement = firstLottoRow.child(3);
     Integer bonusBall = Integer.valueOf(bonusElement.text());
     System.out.println("Bonus ball: " + bonusBall);
     //Elements elementsHtml = doc.getElementsByTag("main-article-content");
     final int SIZE = 7;
     //array to store user numbers
     int [] userNumbers = new int[SIZE];
     boolean found = false;
     int pos = 0;
     int search = 0;
     int searchPos=-1;
     boolean bonus = false;
     int lottCount;
     while (pos<SIZE)
     {
        System.out.println("enter your numbers");
        userNumbers[pos]=keyboard.nextInt();
        pos++;
     }
     for (int count: userNumbers)
     {
        System.out.println(count);
     }
     while ((pos < SIZE) && (!found))
     {
        if (userNumbers[pos] == numbers[0])
        {
           found = true;
           System.out.println("You have matched one number"); //am i wrong in saying  //this?
        }else pos++; //am i incrementing the wrong counter and at what point do i //implement the lottery counter?

     }//while
     if (!found)
     {
        System.out.println("You have not won this time");
     }else if (userNumbers[pos] == bonusBall)
     {
        bonus = true; //i think this is wrong too
     }
     //how do i go about working out how many nos the player has matched or how many //numbers theyve matched plus the bonus?

Upvotes: 0

Views: 1649

Answers (4)

Trevor Freeman
Trevor Freeman

Reputation: 7232

You should put all of your data from your arrays into a Set (such as a HashSet), and then use Google Guava Sets.intersection(set1, set2) to find the intersection between the two sets (the set of all elements that are the same).

This will let you know exactly how many numbers matched, and which numbers they were in only a handful of lines of code.

E.g.

Set<Integer> lottoSet = Sets.newHashSet(Ints.asList(numbers));
Set<Integer> userSet = Sets.newHashSet(Ints.asList(userNumbers));
Set<Integer> matchedElements = Sets.intersection(lottoSet,userSet);

// How many numbers matched
int numMatched = matchedElements.size();

The bonus ball can be handled with an additional simple userSet.contains(bonusBall) check.

Upvotes: 0

bmoran
bmoran

Reputation: 30

First you will need a method to compare the first 6 integers. The order does not matter so you will need to check if the lotto number matches any of the numbers on your ticket. The simplest way to do this would be a for loop within a for loop. Each time check to see if the lotto number matches any of your six numbers, then check the next lotto number against your six numbers. In the event that you have a match, increment a counter int count = 0; // initialize outside for loops if(myNum[x] == lottoNum[x]){ count = count + 1; }

Now you will know how many of the first six numbers are matches. Now create another method to see if bonus matches. This is simply just bool bonusHit; if(myNums[7] == lottoNum[6]){ bonusHit = true;} else{ bonusHit = false;}

Upvotes: 0

William Morrison
William Morrison

Reputation: 11006

Iterate over your user array. Compare each value in that array with the drawn lottery numbers. Increment a counter each time both the numbers match.

Then check the powerball.

Then print out the increment value, and whether the powerball matched.

Upvotes: 0

Daniel Kaplan
Daniel Kaplan

Reputation: 67310

I think an array of Integers is the wrong data type for your problem. Instead, you should use a Set (I'm assuming every number is unique when I say that). Once you've done that, the method you'll want to call is called containsAll. If your numbers are not unique, then use a List instead. It also has a containsAll method. This snippet of code should help you get started:

package com.sandbox;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class Sandbox {

    public static void main(String[] args) throws IOException {
        Set<Integer> userNumbers = new HashSet<>();
        userNumbers.add(1);
        userNumbers.add(2);
        userNumbers.add(3);
        userNumbers.add(4);
        userNumbers.add(5);
        userNumbers.add(6);

        Set<Integer> lotteryNumbers = new HashSet<>();
        lotteryNumbers.add(1);
        lotteryNumbers.add(2);
        lotteryNumbers.add(3);
        lotteryNumbers.add(4);
        lotteryNumbers.add(5);
        lotteryNumbers.add(6);
        lotteryNumbers.add(7);

        if (lotteryNumbers.containsAll(userNumbers)) {
            System.out.println("We have a winner!");
        } else {
            System.out.println("Sorry, you're a loser");
        }
    }


}

Upvotes: 1

Related Questions