Hi all, I'm new to java and here is my code. It runs, but the output is not what I wanted. This project allows the user to enter up to 6 lottery tickets and I have to generate random numbers 0-49. The number generate fine, but the number of tickets entered doesn't. If i enter two tickets, It'll still print out six. It should print out the number of tickets based on user input. Please help. Thank you. My teacher does not allow us to use arrays for this project. For extra credit we can use 2d arrays, but no arraylists. How would I rewrite this code just using 2d arrays?

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

public class LotteryTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
{

ArrayList<Integer> numbers = new ArrayList<Integer>();

System.out.println("Please enter the number of tickets you want");
int numtix = input.nextInt();

for(int i = 0; i <= 49; i++){ //loop to choose 
numbers.add(i+0);
}
for (int i=0; i<6; i++) { //loop for # of lottery tickets 
Collections.shuffle(numbers); //randomly shuffle elements of arraylist
System.out.print("\nLotto Ticket #"+(numtix)+": ");

for (int j=0; j<6; j++) { // determine how many numbers for each ticket
System.out.print(numbers.get(j) + " ");
}
}
}
}
}

Recommended Answers

All 6 Replies

for (int i=0; i<6; i++) { //loop for # of lottery tickets

Above will always print 6 tickets as you set it to 6.

for (int i=0; i<numtix; i++) { //loop for # of lottery tickets

This will print the number of tickets you need from the input of the user.

for(int i = 0; i <= 49; i++){ //loop to choose 
   numbers.add(i+0);
}

Why do you do this?
Why not just?

for(int i = 0; i <= 49; i++){ //loop to choose 
   numbers.add(i);
}

My teacher does not allow us to use arrays for this project. For extra credit we can use 2d arrays, but no arraylists. How would I rewrite this code just using 2d arrays?

I am unclear with this statement. Why do you need to use a 2d array when your array only store 1 set of numbers for 1 purpose? If you want an easy way out, that uses 2d arrays, then why not do something dumb like:

for (int i = 0; i < 5 ; i++){
   for (int j = 0; j < 10; j++){
      numbers[i][j] = (i * 10) + j
   }
}

Oh thank you so much, I got it to work, and it was much more simple than I thought. As for the 2d array thing, it's just a separate assignment. She offered us extra credit to write a code that produces the same output , but instead of using arraylists we can only use 2d arrays. A minor problem would be limiting the number of tickets a user can enter. The limit should be 6, how would I do that?

You can do something like this:

System.out.println("Please enter the number of tickets you want");
int numtix = 0;

while (true){
    numtix = input.nextInt();
    if (numtix <= 6 && numtix >= 0){
        break;
    }

    System.out.println("Max number of tickets you can purchase is 6.\nPlease enter the number of tickets you want");

}

Nevermind I got it! Thanks a lot!. Now to do this with only 2d arrays...

Is there a method to make the output of the numbers in numerical order?

If you have "n" tickets each with "m" numbers on it, you can represent that as a 2D array [n][m] ...
The Collections class has methods you can use to sort a list of numbers into order...

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.