• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing Arrays  RSS feed

 
B.J. Martin
Greenhorn
Posts: 12
Java Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i need help comparing two array lists. For this program i am comparing 2 array lists. The list is integers entered by the user the second is random generated numbers. So far in my program i am able to compare the 2 arrays together and output if they are equal or not however i need the program to output even if atleast one if the integers match, EXAMPLE list one: 1, 2 ,3 ,4, 5. LIST TWO: 1, 3, 3, 3, 3. Since the first number matches i want it to out put there is one match, so on and so forth with if there are 3 or 4 matching integers. here is my code so far. Please Help.

public static void main(String[] args)
{
final int NbrsEntered = 5; //Number of guessed numbers entered
final int LOTTOnbr = 5;
int[] numbers = new int[NbrsEntered];
int[] randomNum = new int[LOTTOnbr];
//int[] TestArrayOne = { 1, 2, 3, 4, 5 };
//int[] TestArrayTwo = { 1, 2, 3, 3, 5 };
boolean arraysEqual = true;
int index = 0;

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

//Generate random Numbers
Random RanNum = new Random();

/** if (TestArrayOne.length != TestArrayTwo.length)
arraysEqual = false;
while (arraysEqual && index < TestArrayOne.length)
{
if (TestArrayOne[index] != TestArrayTwo[index])
arraysEqual = false;
index++;
}
if(arraysEqual)
System.out.println("Equal");
else
System.out.println("Not");
*/



//Enter the 5 lottery numbers
for (index = 0; index < NbrsEntered; index++)
{
System.out.print("Enter digit " + (index + 1) + ": ");
numbers[index] = keyboard.nextInt();
}

//Generate the random lottery numbers
for (index = 0; index < NbrsEntered; index++)
{
System.out.println();
randomNum[index] = RanNum.nextInt(10);
}
System.out.println("Lottery numbers: ");
for (int randomNumElement : randomNum)
{
System.out.print(randomNumElement + " ");
} System.out.println();



//Output player numbers
System.out.println("Player numbers: ");

for (int numbersElement : numbers)
{
System.out.print(numbersElement + " ");

} System.out.println();

//Comparing the 2 arrays
if (numbers.length != randomNum.length)
arraysEqual = false;
while (arraysEqual && index < numbers.length)
{
if (numbers[index] != randomNum[index])
arraysEqual = false;
index++;
}
if(arraysEqual)
System.out.println("Equal");
else
System.out.println("0 matching numbers better luck next time");

}
 
Oceas Anderson
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One easy method would be to iterate through using a simple for loop after your length comparison statement to guarantee they are the same size. And cycle through comparing the integers at each index on both arrays if a match is found then simply increment a int sum.

Also try to UseCodeTags to make it easier for your code to be viewed.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first problem is that you never enter the while loop

You set arraysEqual to false and then test if it is true.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B.J. Martin wrote:Since the first number matches i want it to out put there is one match, so on and so forth with if there are 3 or 4 matching integers. here is my code so far....

OK, a few things:
1. Remove all those commented-out lines - at least when you're posting code here - we like to see what it does, not what it used to do.

2. You've just described above exactly what you want, so why not write a method to do it? For example:
public static int numberOfMatches(int[] array1, int[] array2) { ...
You'll find that things work a lot better if you break them up into small pieces rather than coding everything in main().

3. Your arrays would appear to have something to do with Lotto numbers, yet one of your test arrays is { 1, 2, 3, 3, 5 }. That can't possibly happen in a lottery, since all the numbers selected are distinct.

4. You'll find it easier to compare lottery numbers if the arrays are in the same order. You might want to have a look at the Arrays class (java.util.Arrays) for a method that might help.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!