# Lotto Problem logic error

• May 10th, 2012, 01:14 AM
ippo
Lotto Problem logic error
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

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
}
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) + " ");
}
}
}
}
}
• May 10th, 2012, 02:27 AM
ridg18
Re: Lotto Problem logic error
hi

your fault is int the loop "for (int i=0; i<6; i++) { //loop for # of lottery tickets"

here you say that you want to make a loop 6 times but what if the user want only 2 tickts?

cause of that you got the parameter numtix so why you dont use it??
so try this "for (int i=0; i<numtix; i++){ //loop for # of lottery tickets "

and change "System.out.print("\nLotto Ticket #"+(numtix)+": ");" to "System.out.print("\nLotto Ticket #"+(i+1)+": ");"

• May 10th, 2012, 02:31 AM
ippo
Re: Lotto Problem logic error
thanks, but while waiting for a reply i realized that was the problem as well. and ended up with this code. Everything works fine now, but what method can I use to make sure the output of numbers is in numerical order?

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

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

System.out.println("How many tickets would you like? You may choose up to 6.");
int numtix = 0;

while (true){ //loop for user entering number of tickets
numtix = input.nextInt();
if (numtix <= 6 && numtix >= 0){
break;
}
}

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

for (int j=0; j<6; j++) { // determine how many numbers for each ticket
System.out.print(numbers.get(j) + " ");
}
}
}
}
}
• May 10th, 2012, 04:02 AM
ridg18
Re: Lotto Problem logic error
try this:

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

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

System.out.println("How many tickets would you like? You may choose up to 6.");
int numtix = 0;

while (true){ //loop for user entering number of tickets
numtix = input.nextInt();
if (numtix <= 6 && numtix >= 0){
break;
}
}

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

for (int j=0; j<6; j++) { // determine how many numbers for each ticket
System.out.print(numbers.get(j) + " ");
}
}
}
}
}
• May 10th, 2012, 04:27 AM
ippo
Re: Lotto Problem logic error
okay it worked. Thanks a bunch. My teacher said for an extra challenge we could try to rewrite this program only using arrays and no arraylists and a 2 dimensional array must be used to store the ticket. What would i change? I'm not familiar with 2d arrays. thanks a lot
• May 10th, 2012, 08:32 AM
ridg18
Re: Lotto Problem logic error
you should define array like this,
lotteryarray[numtix][6]
and insert the numbers in it.
so if the user want 3 tickts the array will be 3*6 the number of the columns is the number of the tickts and the number of the rows is the 6 numbers
• May 10th, 2012, 08:56 AM
KucerakJM
Re: Lotto Problem logic error
To quote Copeg (This is for you ridg18):

Quote:

I highly recommend reading the forum rules and the following:

http://www.javaprogrammingforums.com...n-feeding.html

There is a high chance you just contributed to academic dishonesty...nice going.
ippo, here are a few resources on Multi-dimensional arrays:
Multi-dimensional arrays in Java
Java Multi Dimensions Array - Java Tutorials
Java program to demonstrate multidimensional arrays : ArrayCollections Data StructureJava
• May 10th, 2012, 09:23 AM
ridg18
Re: Lotto Problem logic error
ok,
1. i didnt solve the problem, just assist
2. dont care about academic dishonesty, i got my degree 3 years ago so who cares
• May 10th, 2012, 10:18 AM
copeg
Re: Lotto Problem logic error
@ridg18, if you read the article linked to above, you would understand - and given you don't seem to understand you did not read the article, hence ignored advice from another member (pretty disrespectful standard practice)

Quote:

Originally Posted by ridg18
ok,
1. i didnt solve the problem

I fail to see it that way - you posted code without ANY explanation about what you did, only - and I quote - "try this", and according to the original poster "it worked". Did you encourage them to understand why?

Quote:

Originally Posted by ridg18
2. dont care about academic dishonesty, i got my degree 3 years ago so who cares

What about the learning experience of others? Do you not care that you are helping others cheat? If not, than you are on a pace to get banned from these forums.