Reputation: 9053
I retrieved individual String
values from a database and converted it into a String array by using the String method split()
.
ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
while(set.next())
{
num1 = set.getString(1);
num2 = set.getString(2);
num3 = set.getString(3);
num4 = set.getString(4);
num5 = set.getString(5);
num6 = set.getString(6);
num7 = set.getString(7);
totalLotto += num1 + " " + num2 + " " + num3 + " " + num4 + " " +
num5 + " " + num6 + " " + num7 + " " + "/";
}
String[]listOfNumbers = totalLotto.split("/");
The resulting output is as follow
3 18 27 38 41 45 47
4 7 11 15 22 33 42
2 9 15 23 24 39 44
4 11 16 17 35 39 48
etc
How could I add the above numbers into an array of an array so that I can loop through each individual number?(and subsequently check if they are contained in a list of winning numbers?)
Kind regards
Upvotes: 2
Views: 7611
Reputation: 116908
Why are you using strings at all? This seems to be numbers so integers would be a better match:
List<int[]> listOfNumbers = new ArrayList<int[]>();
while(set.next()) {
int[] numbers = new int[7];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = set.getInt(i + 1);
}
listOfNumbers.add(numbers);
}
You obviously could also use Integer[]
if necessary.
Upvotes: 4
Reputation: 61
ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
List<String[]> tmp = new ArrayList<String[]>();
while(set.next())
{
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);
tmp.add(row);
}
String[][] listOfNumbers = tmp.toArray(new String[tmp.size()][7]);
Upvotes: 1
Reputation: 19500
This is an example, you can change it to your code style
String[] a = {"10 20 30 40 50","100 200 300 400 500","1000 2000 3000 4000"};
String[][] b = new String[a.length][];
for(int i = 0; i<a.length; i++){
b[i] = a[i].split(" ");
}
Upvotes: 1
Reputation: 14649
List<int[]> list = new ArrayList<int[]>();
while (set.next()) {
int[] array = new int[7];
for (int i = 1; i <= 7; i++) {
array[i - 1] = set.getInt(i);
}
list.add(array);
}
Upvotes: 2
Reputation: 462
You can assign the values from the resultSet to an array of Strings and add it to a list.
Example:
ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
List<String[]> lottoList = new LinkedList<String[]>();
while(set.next())
{
String[] currentRow = new String[] {set.getString(1),
set.getString(2),
set.getString(3),
set.getString(4),
set.getString(5),
set.getString(6),
set.getString(7)};
lottoList.add(currentRow);
}
// do whatever you like with lottoList
You can then loop through the lottoList and check how many winning numbers there are.
Upvotes: 3
Reputation: 439
ResultSet set = state.executeQuery("SELECT COUNT(*) FROM numberLotto");
String[][] array = new String[rs.getInt(1)][6];
ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
int i = 0;
while(set.next())
{
array[i][0] = set.getString(1);
array[i][1]= set.getString(2);
array[i][2]= set.getString(3);
array[i][3]= set.getString(4);
array[i][4]= set.getString(5);
array[i][5]= set.getString(6);
array[i][6]= set.getString(7);
i ++;
}
Upvotes: 2
Reputation: 62459
You can use an ArrayList
of String
arrays:
ArrayList<String[]> numbers = new ArrayList<String[]>();
Then further split listOfNumbers[0]
by space and add the result to the collection:
String[] listOfNumbers = totalLotto.split("/");
numbers.add(listOfNumbers[0].split(" "));
So assuming listOfNumbers[0]
is
3 18 27 38 41 45 47
Splitting by space will give you a String[]
with a number in each position.
Upvotes: 2