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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Lotto Problem logic error

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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) + " ");
    }
    }
    }
    }
    }


  2. #2
    Junior Member
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)+": ");"

    hope i was helpfull

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    numbers.add(i);
    }
    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) + " ");
    }
    }
    }
    }
    }

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    numbers.add(i);
    }
    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) + " ");
    }
    }
    }
    }
    }

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  7. #7
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Lotto Problem logic error

    To quote Copeg (This is for you ridg18):

    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

  8. The Following User Says Thank You to KucerakJM For This Useful Post:

    copeg (May 10th, 2012)

  9. #8
    Junior Member
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  10. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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 View Post
    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 View Post
    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.
    Last edited by copeg; May 10th, 2012 at 10:39 AM.

Similar Threads

  1. Can any one have some logic for this problem?
    By verma86 in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 09:40 AM
  2. Serial Programming Logic Error???
    By bczm8703 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 27th, 2011, 03:28 AM
  3. Problem with the Logic
    By sudh in forum Java Theory & Questions
    Replies: 4
    Last Post: March 31st, 2011, 07:21 AM
  4. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM
  5. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM