I have an assignment where I have to have seven different lottery numbers ranging from 1 to 35. A separate powerball number should also be generated. The powerball number should be within the range of 1-10. I have to write a java program that gives eight sets of seven numbers plus one powerball number. It has to have an array and the random() method has to be used and at least one repetition statement. I got it to give me one set of seven and a powerball number. I tried to do a "while...do" statement but it created an infinite loop until I stopped the process. A very, very fast infinite loop. Here's what I have I just need to figure out what I need to do in order to loop and create a set of 8. I tried the "For" loop statement but I wasn't sure where in the code it needed to go. Maybe it has to be rewritten completely, I don't know. I'm new to java and I'm doing the best I can. Here's what I have, I'm at a loss:

import java.util.Collections;
import java.util.ArrayList;

public class Lottery //Declare the class
{

	public static void main(String[] args)
	{
		//To display set of seven numbers between 1 - 35
		ArrayList<Integer> numbers = new ArrayList<Integer>();
		for (int i = 0; i < 35; i++)

		{
			numbers.add(i + 1);
		}
	
		Collections.shuffle(numbers);
		System.out.print("This week's numbers are: "); //This displays the first 7 set of random numbers
		for (int j = 0; j < 7; j++)

		{	
			System.out.print(numbers.get(j) + " "); 
		}
		
		//To display a powerball number between 1 - 10
		ArrayList<Integer> pNum = new ArrayList<Integer>();
		for (int p = 0; p < 10; p++)
		{
			pNum.add(p + 1);
		}
	
		Collections.shuffle(pNum);
		System.out.print("The powerball number is: "); //This displays the random powerball number
		for (int q = 0; q < 1; q++)
		{
			System.out.print(pNum.get(q) + " ");
		}

	}
}

Recommended Answers

All 42 Replies

Read up on nested loops.

You need a loop within a loop and you should have a 2 Dimensional Array.

Show me what you come up with or your logic, and I will help you.

Just don't ask straight up for the answer.

Ask as many questions as you want. Even if they are silly. It's the only way to learn.

Try and Write the pseudo code or make a flowchart.

Okay here's what I came up with instead but now it'll let me build the class but when I run it I can an exception in thread error. Here's the code:

import java.util.Random;

public class U3IP3_Woodford //Declare the class
{
	public static void main(String[] args)
	{
		Random randLotto = new Random(); //Declare randLotto as Random
		int pick; // Declare pick as integer
		int draw[] = new int[7]; //Declare an array to hold 7 random numbers
		int pwrBall = 10; // The range for the Power Ball
		int BallNum = (int) (pwrBall * Math.random()) + 1; //Create a power ball Random Number
		
		// Randomly pick seven numbers from 1 to 35

		for (pick = 0; pick <= 7; pick ++)
		
		++draw[ 1+ (randLotto.nextInt(35))];

		// Print output of each ball and powerball

		System.out.printf("%4d%10d\n", draw[pick], BallNum);
		
	}
}

Here's the error that I'm getting:

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at U3IP3_Woodford.main(U3IP3_Woodford.java:17)"

++draw[ 1+ (randLotto.nextInt(35))];

How large is the array "draw"? The max index into that array is the size - 1!
What is the value of the index into that array: 1+ (randLotto.nextInt(35))
Is it less than the size of the array?

What is that statement supposed to do?

If you want to accumulate 7 numbers in the range 1 to 35, use pick as the index to draw and save each random number in the indexed element of draw.

Try this

draw[pick] =  1+ (randLotto.nextInt(6));

instead of

++draw[ 1+ (randLotto.nextInt(35))];

I'm still getting that error that says, "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at U3IP3_Woodford.main(U3IP3_Woodford.java:17)"

It lets me build it into the class but it doesn't let me run it once its built. I get the above error. I'm not sure what it means.

sorry didn't see that
try

for (pick = 0; pick <= 6; pick ++)

or

for (pick = 0; pick < 7; pick ++)

I'm still getting the error. Thank you for helping me with this. I know why (technically) I'm getting the error, but I don't know where in my code its causing the error.

Instead now its saying, "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at U3IP3_Woodford.main(U3IP3_Woodford.java:21)

What statement is at line 21 in your program?
Is this it?
System.out.printf("%4d%10d\n", draw[pick], BallNum);

What is the value of pick here? Does the draw[] array have an element at the value of pick? Remember array's are 0 based so the index to an array goes from 0 to the number of elements minus one. So if draw has seven elements, the max index is 6.

I had changed the pick to both

for (pick = 0; pick <= 6; pick ++)

and

for (pick = 0; pick < 7; pick ++)

but it didn't fix anything and I got the same error.

Please show the lines of code at the line where the error occurs.
Your only showing one line and its NOT the line with the error!!!
Best would be to copy the FULL text of the error message and paste it here.

As NormR1 said, please post your current code, Maybe you made a syntax error or logic error somewhere.

You had said basically that I had to choose less then the size of the array which is 7 so I did <=6 in the code I posted to show that I did do less than the array size. Here's my code now:

import java.util.Random;

public class U3IP3_Woodford //Declare the class
{
	public static void main(String[] args)
	{
		Random randLotto = new Random(); //Declare randLotto as Random
		int pick; // Declare pick as integer
		int draw[] = new int[7]; //Declare an array to hold 7 random numbers
		int pwrBall = 10; // The range for the Power Ball
		int BallNum = (int) (pwrBall * Math.random()) + 1; //Create a power ball Random Number
		
		// Randomly pick seven numbers from 1 to 35

		for (pick = 0; pick <= 7; pick ++)
		
		draw[pick] =  1+ (randLotto.nextInt(6));

		// Print output of each ball and powerball

		System.out.printf("%4d%10d\n", draw[pick], BallNum);
		
	}
}
for (pick = 0; pick <= 7; pick ++)

Take out the = in <=

I think you are getting an out of bounds error because of the line

++draw[ 1+ (randLotto.nextInt(35))];

Since the size of the array is 7, the only accepted numbers in the [] brackets range from 0-6 (inclusive). but you are randomizing a number between 1 and 35 which.. well increase your chance of getting an error.

So instead, I decided to make a slight change. I made another variable to store the random lottery numbers 1 at a time. hence the full code becomes:

import java.util.Random;

public class test //Declare the class
{
	public static void main(String[] args)
	{
		Random randLotto = new Random(); //Declare randLotto as Random
		int pick; // Declare pick as integer
		int draw[] = new int[7]; //Declare an array to hold 7 random numbers
		int pwrBall = 10; // The range for the Power Ball
		int BallNum = (int) (pwrBall * Math.random()) + 1; //Create a power ball Random Number
		
		// Randomly pick seven numbers from 1 to 35
		for (pick = 0; pick < 7; pick ++)
		{
			int number = (int) (35 * Math.random()) + 1;
			draw[pick] = number;
		

		// Print output of each ball and powerball

		System.out.printf("%4d%10d\n", draw[pick], BallNum);
		}
	}
}

Hope this helped.

And also this works but since it is assignment the use the second one to convince your prof. The reason is here

ArrayList<Integer> numbers = new ArrayList<Integer>();
List<Integer> numbers = new ArrayList<Integer>();
for (pick = 0; pick <= 7; pick ++)
 
		draw[pick] =  1+ (randLotto.nextInt(6));
 
		// Print output of each ball and powerball
 
		System.out.printf("%4d%10d\n", draw[pick], BallNum);

What is the value of pick when you exit the for loop?
Change the code to this to see:

for (pick = 0; pick <= 7; pick ++)
 
		draw[pick] =  1+ (randLotto.nextInt(6));
 
		// Print output of each ball and powerball
                System.out.println("pick=" + pick); // What is pick now???
		System.out.printf("%4d%10d\n", draw[pick], BallNum);

NormR1 Please change this

for (pick = 0; pick <= 7; pick ++)

to

for (pick = 0; pick < 7; pick ++)

NOTE the = sign in <=

I'm at work but as soon as I get home I will take into account everything that was suggested. Thank you everyone for helping me so diligently. I'll post if it works and which one worked by posting the code. Again thank you guys. You're all helping me learn so much more than how the teacher explains it.

I think you are getting an out of bounds error because of the line

++draw[ 1+ (randLotto.nextInt(35))];

Since the size of the array is 7, the only accepted numbers in the [] brackets range from 0-6 (inclusive). but you are randomizing a number between 1 and 35 which.. well increase your chance of getting an error.

So instead, I decided to make a slight change. I made another variable to store the random lottery numbers 1 at a time. hence the full code becomes:

import java.util.Random;

public class test //Declare the class
{
	public static void main(String[] args)
	{
		Random randLotto = new Random(); //Declare randLotto as Random
		int pick; // Declare pick as integer
		int draw[] = new int[7]; //Declare an array to hold 7 random numbers
		int pwrBall = 10; // The range for the Power Ball
		int BallNum = (int) (pwrBall * Math.random()) + 1; //Create a power ball Random Number
		
		// Randomly pick seven numbers from 1 to 35
		for (pick = 0; pick < 7; pick ++)
		{
			int number = (int) (35 * Math.random()) + 1;
			draw[pick] = number;
		

		// Print output of each ball and powerball

		System.out.printf("%4d%10d\n", draw[pick], BallNum);
		}
	}
}

Hope this helped.

Yours is the only one that gave me results however the result looks like this:

2 5
11 5
1 5
10 5
12 5
4 5
11 5

The results are supposed to have be a row of seven random numbers (between 1 - 35) with a random powerball number (between 1 -10) after the seven numbers. And there's supposed to be eight sets. So the result should look something like this:

2 4 6 7 8 12 14 5
3 5 7 8 9 13 15 4
6 8 10 13 16 19 23 7
8 13 17 21 24 28 30 10
9 15 16 18 22 25 27 8
10 13 18 19 22 27 29 2
4 6 13 15 27 30 33 1
1 7 12 16 23 32 35 6

Why are there only 2 numbers on a line instead of 7? Can you explain that?
Can you show the print statement you are now using?

Why are there only 2 numbers on a line instead of 7? Can you explain that?
Can you show the print statement you are now using?

import java.util.Random;

public class U3IP3_Woodford //Declare the class
{
	public static void main(String[] args)
	{
		Random randLotto = new Random(); //Declare randLotto as Random
		int pick; // Declare pick as integer
		int draw[] = new int[7]; //Declare an array to hold 7 random numbers
		int pwrBall = 10; // The range for the Power Ball
		int BallNum = (int) (pwrBall * Math.random()) + 1; //Create a power ball Random Number
		
		// Randomly pick seven numbers from 1 to 35

		for (pick = 0; pick < 7; pick ++)
		{
			int number = (int) (35 * Math.random()) + 1;
			draw[pick] = number;		
					
		
		// Print output of each ball and powerball

		System.out.printf("%4d%10d\n", draw[pick], BallNum);
		}			
		
	}
}

System.out.printf("%4d%10d\n", draw[pick], BallNum);

This line will print 2 numbers on a line.

You do not have any code that will print 7 numbers on a line.

im going to re write a part of the program then. gimme 5 minutes.

NormR1, that's the print code I've been using since I started getting help. How do I get it to print 7? And how do I get the powerball numbers to be random as well.

Where are the 7 numbers that you want to print on a line?
If they are in an array, use print() in a loop to print them one by one on a line in and after the end of the loop use println("") to go to the next line.

Done :D Enjoy :) You have to use a double for loop. 1 to choose random numbers 8 times. and 2 for picking the 7 random numbers.

Here's your final code which I think should do:

import java.util.Random;

public class U3IP3_Woodford //Declare the class
{
	public static void main(String[] args)
	{
		int pick; // Declare pick as integer
		 //Declare an array to hold 7 random numbers.
		 //This array will be re-written on after each line is completely printed.
		int draw[] = new int[7];
		int pwrBall = 10; // The range for the Power Ball
		int BallNum=0;
		
		// Randomly pick seven numbers from 1 to 35

		for (int i = 0; i <=8; i ++)
		{
			BallNum = (int) (pwrBall * Math.random()) + 1; //Create a power ball Random Number
			for (pick = 0; pick < 7; pick ++)
			{	//pick a random number
				int number = (int) (35 * Math.random()) + 1;
				draw[pick] = number;//Add the number to the array
				
				// Print output of each ball
				System.out.printf(draw[pick]+"-");
			}		
			System.out.printf(BallNum + "\n");//Print Powerball
			
		}
	}
}

I actually think you dont need the array to store the random numbers. you just directly printf(random number) seeing that the array will be re-written on anyways. If you want I can give you the code for the program WITHOUT using the array.

The only minor thing is that the first seven numbers have to be unique. The powerball doesn't have to be unique though.

lol that.. is a hard thing to do. The thing with picking random numbers is that we have no control over how the numbers would end up. I can get it to work so that no number is the same in a single position (you just have to shift the elements in the array a million times. easy stuff) But I don't think its easy to make sure that no number is the same in those 8 lines.

Unless you mean that no number should be the same in one line of random numbers. That can be easily fixed using a while loop which generates a new random number if the previously generated number is already in the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.