Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array help

 
Giancarlo Marcelli
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Giancarlo Marcelli
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1802
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Giancarlo Marcelli
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic