Method to check for duplicate number does not work

Posted 07 May 2012 - 04:08 PM

I'm working on an assignment for a Lottery Quick Pick program that requires me to create the method private boolean numberUsed(int intInNumber) and have it check if a number has already been picked. If it has, then I must look until I find an unused number. I tried calling it in a for statement to check and it was still returning duplicates. I switched to a while statement as below, and am still getting duplicates. I'm sure it's something simple that I'm not seeing, but I'm hoping someone here can see what I can't. Is the problem with the method numberUsed() or is it how I am calling it in the method getQuickPicks() ?

Re: Method to check for duplicate number does not work

Posted 07 May 2012 - 07:59 PM

I changed the numberUsed() method per your suggestion, and it certainly makes sense that that should work, however it still doesn't. I'm certain it's got something to do with my call to the method, but I can't for the life of me see it - could just be that I've been looking too hard and too long, so maybe someone else can see what I can't?

I'll post the class that calls this class as well, maybe that will make it clearer what I'm trying to accomplish versus what I'm getting.

It can also be done with a int[] with small modifications if you are not allowed to use ArrayList

More great code that I wish I could use, but unfortunately the 4 methods that I have coded are required for this assignment. (I was provided with main class along with the declarations of the 3 private methods and told what they should do. It was up to me to figure out what else I needed based on the main class, which is where I came up with the public method getQuickPicks(). Other requirements were No Number could be repeated in the Quick Pick Array and the Array must be sorted.

I realize that the nested while statements get a bit confusing, but I can't seem to figure out a better way to do it, considering the requirements that were set forth. Worse yet, they don't seem to be working :-(

Ah well, I will continue to play with it, perhaps an epiphany will hit me before sleep does...LOL

Thank you for looking and for your suggestions, while I can't use them in this project, I will keep them in mind for future use!

Re: Method to check for duplicate number does not work

Posted 08 May 2012 - 09:50 PM

Unfortunately, I need to work with the helper class that I was given, so I cannot combine the main with my class and make them one. Also, when I was given the private methods, I was given the parameters to take in and what they should return, so I cannot change those either. I continue to play with this, but at this point, all that I've been able to figure out is that when I check for duplicates, the array is empty, and that is why it is not finding duplicates. This doesn't make sense because the array is returning Lottery Picks, so it can't be empty.

The problem seems to be in accessing the array to check for duplicates, but I cannot for the life of me see what I am doing wrong. I have gotten everything to work except the check for duplicates. I have tried numerous loops using examples that I have found (on this site and others) and nothing seems to work. I could be doing it wrong, or is it because of the method I must use to check for them? I also tried Arrays.binarySearch() with no luck. I have figured out that when I call the method and have it check the random number generated, it does not find any values stored in the array (at least as far as I can tell), so therefore, it never finds duplicates, but duplicates are output to the screen (in a pick 4, highest number 9, I will get numbers like 1 2 2 8, 1 1 6 6 , 3 3 3 2, etcÖ
So, I know what itís not doing, I just donít know why. The problem is clearly with my code, but after looking at it for 6 hours last night and another 4 tonight, I am still at a loss for where. If anyone can point me in the right direction, that would be great!

Re: Method to check for duplicate number does not work

Unfortunately, I need to work with the helper class that I was given, so I cannot combine the main with my class and make them one.

The main() I wrote was to test my code. Just delete it and use the main() of your tester

Quote

Also, when I was given the private methods, I was given the parameters to take in and what they should return, so I cannot change those either.

Just change them to use your own

Quote

I continue to play with this, but at this point, all that I've been able to figure out is that when I check for duplicates, the array is empty, and that is why it is not finding duplicates. This doesn't make sense because the array is returning Lottery Picks, so it can't be empty.
The problem seems to be in accessing the array to check for duplicates, but I cannot for the life of me see what I am doing wrong.

The big idea, that demonstrate my code, is to pass the number of already checked item to the method that checks for duplicates

Re: Method to check for duplicate number does not work

Posted 09 May 2012 - 12:00 PM

Thank you Paul! Your patience is so appreciated! Once I cleared my head (and code) of all the garbage I had going through it, I was able to see where you were coming from with your code. All is working wonderfully now, HUGE THANKS to you!!!