This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Array help

 
Greenhorn
Posts: 3
  • 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!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • 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
  • 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.
 
Bartender
Posts: 1842
13
Eclipse IDE Java Windows
  • 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
  • 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?
 
lowercase baba
Posts: 12766
51
Chrome Java Linux
  • 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
Posts: 4179
22
IntelliJ IDE Python Java
  • 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.
 
The only cure for that is hours of television radiation. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!