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 20 of 20

Thread: lotto ticket generator help

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default lotto ticket generator help

    i can't get it to loop the number of tickets that the user puts in. and right now i keep on getting an ArrayoutofBoundException:0 in my main. i can't see what's wrong with it

    import java.io.*;
    import java.util.*;
    public class Lotto
    {
          //*********commandLine1()**********
          public static boolean commandLine1(String str)
          {
            if(!str.equalsIgnoreCase("mm") &&
               !str.equalsIgnoreCase("sl")) return(false);
            else                            return(true);
        }
        //*********commandLine2()**********
        public static boolean commandLine2(String str)
        {
            int i = 0;
            while(i < str.length())
            {
                char c = str.charAt(i);
                if(c < '0' || c > '9') return(false);
                i++;
            }
            return(true);
        }
        //**********random()***************
        public static int random(int a, int b)
        {
            return((int)((b - a + 1)*Math.random() + a));
        }
        //**********superLottoNumbers()*********
        public static int[] superLottoNumbers()
        {
            System.out.println("       Super Lotto      ");
            System.out.println("                         Mega");
            int numZ[] = new int[6];
            for(int h = 0; h <=4; h++)
            {
                numZ[h] = random(1,47);
            }
            numZ[6] = 99;
            Arrays.sort(numZ);
            numZ[6] = random(1,27);  
            return(numZ);
        }
         //**********megaMillionNumbers()*********
        public static int[] megaMillionNumbers()
     
      {
            System.out.println("       Mega Million Lotto      ");
            System.out.println("                      Mega");
            int numZ[] = new int[6];
            for(int i = 0; i <= 4; i++)
            {
                numZ[i] = random(1,56);
            }
            numZ[5] = 99;
            Arrays.sort(numZ);
            numZ[5] = random(1,46);
            return(numZ);
        }
        public static void main(String args[])
        {
            if(commandLine1(args[0]) == false) System.err.println("Not a valid type of Lottery");System.exit(1);
            if(commandLine2(args[1]) == false) System.err.println("Not a valid number of tickets");System.exit(2);
            if(args[1].equals("0"))           System.err.println("Can't print out 0 ticekts.");System.exit(3);
            if(args[0].equalsIgnoreCase("sl"))
            {
              int num[] = new int[7];
              for(int k = 0; k <Integer.parseInt(args[1]) ; k++)
              {
                 for(int h = 0; h < 6 ; h++)
                 {
                    num = superLottoNumbers();  
                    System.out.print(num[h]+"   ");
     
      }
              }
            }
           /* if(args[0].equalsIgnoreCase("mm"))
            {
                int num[];
                for(int k = 0; k <Integer.parseInt(args[1]); k++)
                {
                    num = megaMillionNumbers();
                    for(int i: num) System.out.print(i+"   ");
                }
            }*/
        }
    }
    Last edited by tlckl3m3elmo; July 14th, 2011 at 06:47 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: lotto ticket generator help

    Please repost your code between tags (see BB code link below) to retain formatting and make it easier to read.

    Copy and paste the full and exact error message. Indicate on which line it occurs.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: lotto ticket generator help

    (args[0]) == false)
    Use the equals() method when comparing Strings. The == operator is for primitives.

    Where do you test if there are any elements in the args array?
    Last edited by Norm; July 14th, 2011 at 07:48 PM. Reason: if vs it

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lotto ticket generator help

     
    import java.io.*;
    import java.util.*;
    public class Lotto
    {
        //*********commandLine1()**********
        public static boolean commandLine1(String str)
        {
            if(!str.equalsIgnoreCase("mm") &&
               !str.equalsIgnoreCase("sl")) return(false);
            else                            return(true);
        }
        //*********commandLine2()**********
        public static boolean commandLine2(String str)
        {
            int i = 0;
            while(i < str.length())
            {
                char c = str.charAt(i);
                if(c < '0' || c > '9') return(false);
                i++;
            }
            return(true);
        }
        //**********random()***************
        public static int random(int a, int b)
        {
            return((int)((b - a + 1)*Math.random() + a));
        }
        //**********superLottoNumbers()*********
        public static int[] superLottoNumbers()
        {
            System.out.println("       Super Lotto      ");
            System.out.println("                         Mega");
            int numZ[] = new int[6];
            for(int h = 0; h <=4; h++)
            {
                numZ[h] = random(1,47);
      }
            numZ[6] = 99;
            Arrays.sort(numZ);
            numZ[6] = random(1,27);
            return(numZ);
        }
         //**********megaMillionNumbers()*********
        public static int[] megaMillionNumbers()
        {
            System.out.println("       Mega Million Lotto      ");
            System.out.println("                      Mega");
            int numZ[] = new int[6];
            for(int i = 0; i <= 4; i++)
            {
                numZ[i] = random(1,56);
            }
            numZ[5] = 99;
            Arrays.sort(numZ);
            numZ[5] = random(1,46); 
            return(numZ);
        }
        public static void main(String args[])
     {
            if(commandLine1(args[0]) == false) System.err.println("Not a valid type of Lottery");System.exit(1);
            if(commandLine2(args[1]) == false) System.err.println("Not a valid number of tickets");System.exit($
            if(args[1].equals("0"))           System.err.println("Can't print out 0 ticekts.");System.exit(3); $
            if(args[0].equalsIgnoreCase("sl"))
            {
              int num[] = new int[7];
              for(int k = 0; k <Integer.parseInt(args[1])-1 ; k++)
              {
                 for(int h = 0; h < 6 ; h++)
                 {
                    num = superLottoNumbers();
                    System.out.print(num[h]+"   ");
                 }
              }
            }
        }
    }

    error:
    /students/mau1> java Lotto sl
    /students/mau1> java Lotto
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Lotto.main(Lotto.java:67)
    Last edited by tlckl3m3elmo; July 14th, 2011 at 07:44 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: lotto ticket generator help

    Have you solved the problem or do you have more questions?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: lotto ticket generator help

    ava.lang.ArrayIndexOutOfBoundsException: 0
    at Lotto.main(Lotto.java:67)
    Go read the last line of post #3

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lotto ticket generator help

    i haven't solved it. I think there's something wrong with my for loop to generate the number of lotto tickets.
    it worked up until i tried putting in the loop.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: lotto ticket generator help

    there's something wrong with my for loop
    Is there an error? Please post the full text of the error message.

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lotto ticket generator help

    oh sorry norm. and itts not comparing 2 strings. i have the boolean method, but that doesnt matter for my loop.

    and i test my code in Hills server at my college using SSH.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: lotto ticket generator help

    Is there an error? Please post the full text of the error message.

    i test my code in Hills server at my college using SSH.
    What does that mean?
    Are you able to execute the code and get the error messages that are generated?

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lotto ticket generator help

    i did post it

    /students/mau1> java Lotto
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Lotto.main(Lotto.java:67)

    this is my error when i type "java Lotto" in my command line

  12. #12
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lotto ticket generator help

    Quote Originally Posted by Norm View Post
    Is there an error? Please post the full text of the error message.



    What does that mean?
    Are you able to execute the code and get the error messages that are generated?
    uh you asked where i test my methods. I compiled it with no error messages. but when i try to run the program it gives me the arrayout of bound exception:0 msg

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: lotto ticket generator help

    it gives me the arrayout of bound exception:0 msg
    Please post the full text of the error message.

    Let me change my question:
    Where do you check if there are any elements in the args array?

    java.lang.ArrayIndexOutOfBoundsException: 0
    at Lotto.main(Lotto.java:67)
    This error message says you are trying to look at the first element in an empty array.

  14. #14
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lotto ticket generator help

    Quote Originally Posted by Norm View Post
    Please post the full text of the error message.

    Let me change my question:
    Where do you check if there are any elements in the args array?


    This error message says you are trying to look at the first element in an empty array.
    why do you keep on asking me for the full text of the error message? i already did! thats all it says to me. and what do you mean where do i check if there are any elements in the args array?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: lotto ticket generator help

    what do you mean where do i check if there are any elements in the args array?
    Your code is trying to look at the first element. The array is empty.
    Your code must test/check if that array is empty and do something besides get an error.
    Where do you do that check? The answer obviously is that you don't check for an empty array.

  16. #16
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lotto ticket generator help

    Oh like i didn't type anything?

    Well I tried typing "java Lotto sl" it just gives me a blank. sl should be my first element for my array args[]

  17. #17
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: lotto ticket generator help

    The error is occuring on line 67. You need to go to that line, see what array you are trying to read and trace back as to why it has a length of 0.

    Also your code is very confusing.
    for(int k = 0; k <Integer.parseInt(args[1]) ; k++) {
        for(int h = 0; h < 6 ; h++) {
            num = superLottoNumbers();
            System.out.print(num[h]+" ");
        }
    }
    Why do you have nested loops? Why are you creating a new array of lotto numbers each time around the inner loop? When h is 0 you creaet an array of numbers and print out the number in position 0. Then h becomes 1 and you create a new array of numbers (throwing away the first set) and print out the number in position 1. Then h becomes 2 etc. Why aren't you creating the array of lotto numbers once?
    Improving the world one idiot at a time!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: lotto ticket generator help

    Two comments:
    Add this line first thing in main method for ease of testing:
            args = new String[] {"sl", "3"}; // hard code for testing

    Don't hard code any numbers for array indexes. Use the properties of the array itself:array.length
    as appropriate.
    I see lots of 4 and 5 and 6 used as indexes.

  19. #19
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lotto ticket generator help

    sorry guys. i just got back from my class.

    ok the point of this program is to type "java Lotto (mm or sl, which is the type of lotto) (#of tickets)" in the command line
    and the output is supposed to be exactly like a Super Lotto ticket except the date and time and price. the problem i'm having is creating a loop that will print out multiple lines of 5 random unique numbers and 1 random number.

    ok thanks so far guys i will try what you guys have said

  20. #20
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: lotto ticket generator help

    What you need to do is walk away from the computer. Grab some paper and a pencil. Then draw out what you need to do. Fully plan each tiny itsy-bitsy little step you need to take. Once you are 110% confident you know exactly what the code should do can you go back to the computer and start translating your detailed design into code.
    Improving the world one idiot at a time!

Similar Threads

  1. help with GUI password generator
    By semicolon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 30th, 2011, 12:22 PM
  2. Replies: 0
    Last Post: January 25th, 2011, 12:24 AM
  3. Loopy Lotto....
    By JavaCow in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 18th, 2010, 06:28 PM
  4. new program (lotto.class)
    By james137 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 3rd, 2009, 04:22 PM
  5. Java password generator program to generate 8 random integers
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 16th, 2009, 07:49 AM