# Thread: Need a little help with arrays

1. Member Join Date
Nov 2016
Posts
22
Rep Power
0

## Need a little help with arrays

I have a very hard time understanding arrays. My program is supposed to let the user enter seven numbers, the computer randomly generates seven numbers between 1-39 and then the computer will tell the user which numbers were correct. The thing is I have no idea how to store the correct numbers in a new array, and then print the new array at the end. I've been messing around with all sorts of attempts, but nothing is working. Any guidelines would be highly appreciated!

Java Code:
```package whatever;

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) {

int[] userNumbers = new int;
int[] winningNumbers = new int;
int guesses;
int i;
int counter = 0;

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

System.out.println("Enter seven numbers between 1 and 39 ");

for(i = 0; i < userNumbers.length; i++){
userNumbers[i] = guesses;
// System.out.println(userNumbers[i]);

for(int x = 0; x<7;x++){

if(winningNumbers[x] == userNumbers[i]){

counter+=1;

}

}

}

if (counter == 7){
System.out.println("You won!");
}
else
System.out.println("You had " + counter + " numbers correctly: "  ); //THIS IS WHERE I WANT THE NUMBERS PRINTED

/*System.out.println(Arrays.toString(winningNumbers));
System.out.println(counter);
These two lines are only so I can see what is happening*/

}

}```
Last edited by iwtljava; 11-25-2016 at 04:28 PM. Reason: had to  Reply With Quote

2. ## Re: Need a little help with arrays

What are in the arrays now?
Here is one easy way to print an array's contents:
Java Code:
`System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));`
Post the array's current contents
and add some comments showing what you want to be in the array.  Reply With Quote

3. Member Join Date
Nov 2016
Posts
22
Rep Power
0

## Re: Need a little help with arrays

Nothing is in the array because I always get error messages. Basically, at line 45 I want to make a new array, which consists of elements from line 43 (which compares the user's numbers to the computer's numbers, and if the user has a number correct I want to store it in a new array). So I've tried doing it like

Java Code:
`int[]correctNumbers = winningNumbers[x];`
, but all I get is a Type mismatch: cant convert from int to int[]

And then at line 60 I want to print the new array so I can see which numbers were correctly guessed.  Reply With Quote

4. ## Re: Need a little help with arrays

I want to store it in a new array
See lines 15 and 16 for how to declare a new array.

See lines 29 and 38 for how to store a value in an array.

When the number of elements is not known when the array is created, you need to define an array that is large enough to hold all the possible elements that will be added. Then to keep track on the number of elements in the array there needs to be an int value that holds the current number of elements in the array.

Those problems have been solved with the ArrayList class.  Reply With Quote

5. Member Join Date
Nov 2016
Posts
22
Rep Power
0

## Re: Need a little help with arrays

Java Code:
```package whatever;

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) {

int[] userNumbers = new int;
int[] winningNumbers = new int;
int guesses;
int i;
int counter = 0;
int[]correctNumbers=new int;

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

System.out.println("Enter seven numbers between 1 and 39 ");

for(i = 0; i < userNumbers.length; i++){
userNumbers[i] = guesses;
// System.out.println(userNumbers[i]);

for(int x = 0; x<7;x++){

if(winningNumbers[x] == userNumbers[i]){

correctNumbers[x] = userNumbers[i];
counter+=1;

}

}

}

if (counter == 7){
System.out.println("You won!");
}
else
System.out.println("You had " + counter + " numbers correctly: " + correctNumbers  ); //THIS IS WHERE I WANT THE NUMBERS PRINTED

/*System.out.println(Arrays.toString(winningNumbers));
System.out.println(counter);
These two lines are only so I can see what is happening*/

}

}```
I added line 20 and 46
Something like this? All it prints now is the place where the array is stored, [I@1b6d3586  Reply With Quote

6. ## Re: Need a little help with arrays

All it prints now is the place where the array is stored, [I@1b6d3586
That is the String returned by the array's toString() method.

The code in line 65 formats and prints the contents of the counter array.

You missed this part of my post:
to keep track on the number of elements in the array there needs to be an int value that holds the current number of elements in the array.
That variable will be used as the index into the array. The variable: counter might work.
Last edited by Norm; 11-25-2016 at 06:41 PM.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•