It's not a secret anymore!
The moose likes Beginning Java and the fly likes Array Comparison doesnt appear to be working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Comparison doesnt appear to be working" Watch "Array Comparison doesnt appear to be working" New topic

Array Comparison doesnt appear to be working

John Paul Hoey

Joined: Apr 15, 2012
Posts: 26
Hi all,

I was wondering if someone could help me please with the following lottery programme ive written.

The programme seems to work correctly until the last part of the code were it always displays that there were no matching numbers, even though there were; im totally confused as to why the array comparison isnt working (please note that i am a complete beginner so be nice )

Any help that is given will be greatly appreciated.

Kind regards

Here is my code:

Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

Looks to me like it should be displaying the matching numbers correctly. Time to test your assumptions by putting in print statements (or using a debugger) to see what the code is actually doing.

I would start by printing out i, j, LotteryNumbers[i], and ActLotteryNumbers [j] in that loop.
Rajat Jindal
Ranch Hand

Joined: Sep 19, 2011
Posts: 44
Hi.. There are several problems that I can see in this program.. I will tell you some of them one by one.. but I would say you should try yourself for such small problems and must take eclipse or some other IDE Debugger's help...

Let's begin with the problem for which you are seeking solution:
From line number 53 to 67, this is the following code..

In the for loop you are using if-else conditional check.. what you are doing here is , you are checking, is first number that you have entered and the first number generated by the system using Random class is same or not and iterating it for other numbers also ..(here is the problem) but in case it wont match the first number it will go to else block... and in else block you have written "NO matching Number this week".. it means this message should be shown on the screen every time whenever the if conditions fails and I believe this is not the right way.. Moreover.. in the very first check only you are going to quit the application using System.exit(0);

According to me your code should be like this:

2. Second problem I can see is for the code

In case the input lies <1 or input >49.. you will enter the if block and you will keep forcing the user to enter number between 1 and 49.. till the moment you achieve the goal using using do-while..
Suppose user enters very first time 78.. then you forced him to put b/w 1 and 49.. and he put a number say 38... but what are you doing with that 38.. you are skipping it.. you are not storing that number in LotteryNumber[] array.. because the code is in else part.. which will never gets executed once it entered into if block... You should find that solution yourself... :-)
John Paul Hoey

Joined: Apr 15, 2012
Posts: 26
Hi Guys,

Thanks for the replies - they have been very helpful indeed.

@Jeff: The online course i have started hasnt mentioned anything about debugging as yet (im sure this has still to come) however the tips you mentioned certainly helped me deduce a few other problems i encountered, especially the fact i had + 1 after each loop of the array (not sure why i included this), your advice also helped me figure out the first problem Rajat identified within my code so thanks again for your feedback.

@Rajat: Your feedback was very valuable so thank you very much for this. You were indeed correct regarding your first point so i have now fixed this using the advice Jeff provided.
However regarding your second point, i included your code into my code and unfortunately it didnt work however it got me thinking and i actually went with a counter instead of a flag and it is now working.

Here is my updated code:

I understand there are a few things i can do to improve this programme however for now it is working as intended so once again thanks very much for your much needed and helpful feedback.


Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
Welcome to the Ranch

I would make the same suggestions for input and length of main method which I made here. JOptionPane is a very old-fashioned form of input; people have used Scanner in preference for the last 7½ years. Get all that code out of the main method. Create a LotteryEntry class and a Lottery class, with int[] fields representing the numbers. Create input methods (which can probably be static) to validate your input.
Get a sheet of paper and work out how you are going to work out whether the numbers in your array are in the Lottery array. The code looks as if you had written code before working out the algorithm you are going to use.
Don’t use System.exit if at all possible. System.exit can be a bit vicious and can do damage under some circumstances, so get out of the habit of using it.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
You are writing procedural code with an object-oriented language. You can get away with this, but your task will become much simpler if you use the power of objects!
John Paul Hoey

Joined: Apr 15, 2012
Posts: 26
Hi Campbell & Dennis, thanks for the feedback.

The problem i have at the moment is that the online course im doing (which is totally self paced and self learning) hasnt introduced me to objects and classes as yet and its only the basics at the moment, however looking at the syllabus it appears im a few chapters away from writing classes and objects therefore once ive learned this ill adopt classes and objects into the above program and make it more object oriented

@ Campbell: Thanks for the tips regarding JOptionPane and System.exit; ill definately heed these points going forward in my programming studies

Thanks again
I agree. Here's the link:
subject: Array Comparison doesnt appear to be working
It's not a secret anymore!