Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need Help Comparing Arrays

1. ## Need Help Comparing Arrays

Hello folks, first post here and also a Full Time student at the University of Cincinnati. Currently in Programming I which is all Java based. Almost done with the first semester, but I've hit a wall on the current program I'm working on. I'd really appreciate some help with it. Basically, I had to create a lottery program that will generate 5 sets of 5 random numbers, then a set of "winning" numbers. Then those need to be compared to see if any match for a winner.

I've got my program generating the numbers, but I just don't know how to build the proper loop or what I need to do to compare two arrays for common data. I have to print the results for the user. This is also rules for my assignment.

All sets must follow these rules:
1. First number (an integer) must be in the range: 1 to 10, second number: 11 to 20, third number: 21 to 30,
fourth number: 31 to 40, fifth number: 41 to 50.
2. No number can be zero
Winners. Let the player know if they win:
1. 3 numbers match = \$100,000
2. 4 numbers match = \$250,000
3. 5 numbers match= JACKPOT!! \$1,000,000
4. Less than 3 numbers matching, no win.

My code is listed below, any help you all can provide would be greatly appreciated. If my formatting or comments suck, please feel free to let me know. But keep in mind, this is my first semester of programming with no prior experience Thank you!!!
```//****************************
// Programmer: Potato McGruff
// March 25th, 2013
// University of Cincinnati
// Programming I
// Auto Lottery Program
//****************************

import java.util.Scanner;

public class AutoLotto { // Start public class AutoLotto
public static void main(String[] args) { // Begin main method.
Scanner input = new Scanner(System.in);

// Welcome message
System.out.println("Welcome to the Auto Lotto, where all your lottery dreams can come true!");
System.out.println("Would you like to try your luck and be our next jackpot winner?");
System.out.print("Press 1 for fun! Press any other number if you're ready to give up! ");
int keepPlaying = input.nextInt();

if (keepPlaying == 1) { // start if loop

// generating number arrays

int[] winningNumbers = {(int)Math.floor(generateRandomNumber()),(int)(Math.floor(generateRandomNumber())+10), (int)(Math.floor(generateRandomNumber())+20), (int)(Math.floor(generateRandomNumber())+ 30), (int)(Math.floor(generateRandomNumber())+ 40)};

int[] numberSetOne = {(int)Math.floor(generateRandomNumber()),(int)(Math.floor(generateRandomNumber())+10), (int)(Math.floor(generateRandomNumber())+20), (int)(Math.floor(generateRandomNumber())+ 30), (int)(Math.floor(generateRandomNumber())+ 40)};

int[] numberSetTwo = {(int)Math.floor(generateRandomNumber()),(int)(Math.floor(generateRandomNumber())+10), (int)(Math.floor(generateRandomNumber())+20), (int)(Math.floor(generateRandomNumber())+ 30), (int)(Math.floor(generateRandomNumber())+ 40)};

int[] numberSetThree = {(int)Math.floor(generateRandomNumber()),(int)(Math.floor(generateRandomNumber())+10), (int)(Math.floor(generateRandomNumber())+20), (int)(Math.floor(generateRandomNumber())+ 30), (int)(Math.floor(generateRandomNumber())+ 40)};

int[] numberSetFour = {(int)Math.floor(generateRandomNumber()),(int)(Math.floor(generateRandomNumber())+10), (int)(Math.floor(generateRandomNumber())+20), (int)(Math.floor(generateRandomNumber())+ 30), (int)(Math.floor(generateRandomNumber())+ 40)};

int[] numberSetFive = {(int)Math.floor(generateRandomNumber()),(int)(Math.floor(generateRandomNumber())+10), (int)(Math.floor(generateRandomNumber())+20), (int)(Math.floor(generateRandomNumber())+ 30), (int)(Math.floor(generateRandomNumber())+ 40)};

// printing sets and winning numbers
System.out.println(" Number Set 1 - " + numberSetOne[0] + " " + numberSetOne[1] + " " + numberSetOne[2] + " " + numberSetOne[3] + " " + numberSetOne[4]);
System.out.println(" Number Set 2 - " + numberSetTwo[0] + " " + numberSetTwo[1] + " " + numberSetTwo[2] + " " + numberSetTwo[3] + " " + numberSetTwo[4]);
System.out.println(" Number Set 3 - " + numberSetThree[0] + " " + numberSetThree[1] + " " + numberSetThree[2] + " " + numberSetThree[3] + " " + numberSetThree[4]);
System.out.println(" Number Set 4 - " + numberSetFour[0] + " " + numberSetFour[1] + " " + numberSetFour[2] + " " + numberSetFour[3] + " " + numberSetFour[4]);
System.out.println(" Number Set 5 - " + numberSetFive[0] + " " + numberSetFive[1] + " " + numberSetFive[2] + " " + numberSetFive[3] + " " + numberSetFive[4]);
System.out.println("");
System.out.println(" Winning Numbers - " + winningNumbers[0] + " " + winningNumbers[1] + " " + winningNumbers[2] + " " + winningNumbers[3] + " " + winningNumbers[4]);

} // end if
else { // start else
System.exit(1);

} // end else
} // end main method

public static double generateRandomNumber() { // begin method generateRandomNumber
double x = Math.random()*10+1;
return x;
} // end generateRandomNumber method

} // end public class Auto Lotto```

2. ## Re: Need Help Comparing Arrays

compare two arrays for common data
What would be the results of a comparison of two arrays? All match or not or a count of the number of matches of corresponding positions or a match of items no matter what their position.

[code=java]
[/code]
to get highlighting and preserve formatting.

3. ## Re: Need Help Comparing Arrays

Ok, fixed that Java code.

Basically I have one array set up, winningNumbers. I need to have it compare to numberSetOne, two, three etc. for matches. Once I know how many have matched I can generate the results to the user. This here is the exact assignment as it was passed on to me.

You are to create a lottery simulation program. Welcome the player to the game and prompt them if they
want to play or exit.
Your program should have a function that automatically generates five sets of five random numbers for the
player and displays them on the screen.
Once the player’s numbers are generated, a separate set of five “computer generated winning numbers” are
displayed a few lines below and identified as the winning draw for the day. The program will then check to see
if the player has won based on whether one of their auto pick sets match 3, 4 or all 5 of the winning numbers.
Give them the option to play as many times as they want with new picks. Winning picks must be in the same
set.

It will look something like this when displayed (these are not winning sets):
Auto-Pick 1 8 12 22 33 45
Auto-Pick 2 5 15 25 34 48
Auto-Pick 3 7 12 28 38 42
Auto-Pick 4 2 13 27 35 49
Auto-Pick 5 1 18 21 33 50
Computer Generated Winning Picks 9 11 29 40 41

All sets must follow these rules:
1. First number (an integer) must be in the range: 1 to 10, second number: 11 to 20, third number: 21 to 30,
fourth number: 31 to 40, fifth number: 41 to 50.
2. No number can be zero

Winners. Let the player know if they win:
1. 3 numbers match = \$100,000
2. 4 numbers match = \$250,000
3. 5 numbers match= JACKPOT!! \$1,000,000
4. Less than 3 numbers matching, no win.

4. ## Re: Need Help Comparing Arrays

need to have it compare to numberSetOne, two, three etc. for matches.
Can you describe what you mean by "matches"? Perhaps an example of a "match" would help.

5. ## Re: Need Help Comparing Arrays

Example, I have random numbers generating into winningNumbers array. If those numbers generated were 9, 13, 22, 36 and 45 I need to reference that to the other 5 arrays (numberSetOne, numberSetTwo, numberSetThree, numberSetFour, numberSetFive) Those arrays are also receiving random number as well.

I need to determine if the random numbers generated match, not 100% but if there are any matches at all. If it matches less than 3, then they player loses. If they match 3 they win the "100,000" prize, if they match 4, 250,000 and 5 the jackpot of 1,000,000.

So I'm looking for something that will compare all the arrays to each other, and then spit out the result for each of the 5 arrays checked. So I would assume I would need some sort of a method or loop made, but that's where I'm stuck.

I hope I'm explaining this better, if not I'm not sure really how else to explain it :/

6. ## Re: Need Help Comparing Arrays

If those numbers generated were 9, 13, 22, 36 and 45
What arrays would have "matches" with that array?
Would a five element array that contained a 22 match or would it be a 20% match?