File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array help" Watch "Array help" New topic
Author

Array help

Giancarlo Marcelli
Greenhorn

Joined: Dec 12, 2012
Posts: 3
Hey there,

I'm in need of some assistance regarding comparing values from two arrays that are in separate class files.
I am writing a Lottery application (I am a student indeed!) and I have two arrays; one for the user inputted values, and one for the randomly generated winning numbers.
I must compare the two, and obviously, see if the user has won the lottery.

I believe I must use an accessor method for the user generated numbers to access an individual array index, and then search through the winning numbers and compare; however, what troubles me is accessing
the user generated numbers. What I think is correct is only producing "0".

Anyone have any ideas? I have searched the forum, but, it seems that it's rare to find others using separate class files so nothing applies.

Here is the code for the method:



Thank you!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I the code snippet you provided - what does line #2 do? What does it contain? What do you expect it to contain?

Then you have a variable names iNumber. What is its value at line #3? How does that value change by the time you get to line #5? How would that value change as the for loop progresses?


Steve
Giancarlo Marcelli
Greenhorn

Joined: Dec 12, 2012
Posts: 3
well, line 2 initializes the array with 6 ints. That's what I've used in other areas of my program. Should I post it all? It's just I have three class files separately.

i presumed that in line 3 iNumber would be populated from the accessor method from the array that iNumber is used in. It's value is 0 at line 3, and it stays at 3 lol.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

Line 2 doesn't initialize the array with 6 ints. It initializes an int array that has 6 places for ints. You never populate winning numbers.

You might think of creating a method that will return an int array that's populated with 6 random numbers.


When you do things right, people won't be sure you've done anything at all.
Giancarlo Marcelli
Greenhorn

Joined: Dec 12, 2012
Posts: 3
the population of the array happens in another method, but i did not post the entire program at the time of the original post.
Shall I do that now?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

It's kind of hard to help you if you don't post all the RELEVANT code. We certainly don't need to see hundreds and thousands of lines of code, but there needs to be enough that we can see what your code is REALLY doing. Even if you just post something like this:



we can see that the array might be filled.

Generally speaking, the easiest thing to do is put System.out.println() statements in your code. from your original snippet, i'd start by putting one in between lines 4 and 5, printing the value of iWinNum. Then also print winNumList[iWinNum]. You can then prove to yourself you are iterating through the winNumList correctly, that it contains the values you think it should, etc.

My guess is that some array ISN'T populated how you think it is. The only way to be sure is to print things out (or use a debugger, but that's a little more advanced).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Giancarlo Marcelli wrote:the population of the array happens in another method, but i did not post the entire program at the time of the original post.
Shall I do that now?


I guess the most direct question is:
Is the array populated AFTER you do in this method? My guess is no - it is not. That you populate an array you store in the winNumList variable, then you call the method shown above and do This would create a NEW array, store it in the same variable (throwing the other array away to be garbage collected). You should not be creating a new array and re-assigning it if the array has already been assigned and filled in.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array help