Lottery match exercise

Go To


I have been busy with a exercise where I need to compare a winning lottery number(which is generated) with existing lottery numbers in a database table.

I manage to loop through all the numbers in the database and subsequently find matches with the winning set of numbers but am struggling to pinpoint in which set of lottery numbers the match was found.
The numbers were placed individually in each database column. Here I retrieve the values in each row.

ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
                String[]row = new String[7]; 
                row[0] = set.getString(1);                   
                row[1] = set.getString(2);
                row[2] = set.getString(3);
                row[3] = set.getString(4);
                row[4] = set.getString(5);
                row[5] = set.getString(6);
                row[6] = set.getString(7);
String[][]listOfNumbers = entries.toArray(new String[entries.size()][7]);

I then loop through listOfNumbers and compare all the values with the values of winningNumber.

Object[]winningNumber = lottoDrawString.toArray();   

        for (int i=0; i < listOfNumbers.length; i++)
            for (int j=0; j < listOfNumbers[i].length; j++)
                for(int k = 0; k < winningNumber.length; k++)

                        System.out.println("There is a match " + listOfNumbers[i][j] + " and " + winningNumber[k]);


How can I retrieve the row where the match(es) were found?

kind regards

2012-04-03 20:51
by Arianule
query the db for the row with the winning number - ControlAltDel 2012-04-03 20:55
Can't help but quote Linus Torvalds here "If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program." . Ok with what I understand, you are doing a lot of heavy lifting in code, can you create a JOIN query in order to check this - ring bearer 2012-04-03 20:56
I don't understand why your algorithm needs to be so complex. Once you have the winning numbers, why can't you just iterate all candidates and use the List containsAll method? This would mean taking the lottoDrawString and converting it into a List, which is pretty simple - Amir Afghani 2012-04-03 21:07
Are you trying to check if a given lottery number contains any digit of the winning number - ring bearer 2012-04-03 21:11


Why not just do

SELECT * FROM numberLotto WHERE FirstNumber = n1 AND SecondNumber = n2 AND ...

For that matter, why not just store the lotto numbers as a seven-digit string in the database?

2012-04-03 21:00
by Taymon
Good point, to - py_script 2012-04-03 21:01
The problem I see with that is there can be many possible matches of three. Say I have number 4 7 13 23 27 33 4, 4,7,13 can match the ticket, 13, 23, 27 can match...etc. etc - Arianule 2012-04-04 06:54


One idea could be to add the line number as table field, retrieve it, and instead of creating and checking numbers, do it with an object, lets say Loterry.

class Lottery

{ int line, value;


You check value field and if it fits your check, return line. Hope it helps :)

2012-04-03 21:00
by py_script
What makes you think there's only one column? It looks to me like there are at least eight columns - Taymon 2012-04-03 21:02
True, I will edit the pos - py_script 2012-04-03 21:03


You can accomplish the search without doing much "programming". Write a query to do it for you:

String query = "SELECT * FROM numberLotto WHERE number = " + winningNumber

The above assumes that your lottery numbers are stored in a field labeled "number". You may also use AND or OR clauses to search for multiple matches.

2012-04-03 21:03
by collinjsimpson