Java 101: SuperLotto

So in Java class today, I got to write a lotto ticket program using an array, a nifty method, some string concatenation and a couple other cool Java tricks.

The spec said to create a getLottoTicket method that would create a 5-element array of random numbers between 1 and 47, then add a “mega” random number between 1 and 27. The main method would let the user choose how many lotto tickets to buy, then print out the resulting tickets.

Here’s what I wrote:

import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;

public class SuperLotto 
    public static String getLottoTicket()
            //create the lottoTicket string and initialize to separator
            String lottoTicket = "*****\n";
            // create random number generator
            Random lottoNumbers = new Random();
            // create an array of 5 random numbers and sort it
            int [] ticket = new int[5];
            for( int counter = 0; counter < ticket.length; ++counter )
                ticket[counter] = (1 + lottoNumbers.nextInt(47) );
            Arrays.sort( ticket );
            // walk through the sorted array,
            // adding each element to the lotto ticket string
            for(int counter = 0; counter < ticket.length; ++counter )
                lottoTicket += ticket[counter];
                lottoTicket += " ";
            //add the mega number & separator to the string and return it
            lottoTicket += "MEGA: ";
            lottoTicket += (1 + lottoNumbers.nextInt(27) );
            lottoTicket += "\n*****\n\n";
            return lottoTicket;

    public static void main( String [] args )
        // create a variable for number of tickets and get user input
        int numberOfTickets;
        Scanner input = new Scanner( );
        System.out.print( "How many tickets do you want? " );
        numberOfTickets = input.nextInt();
        // print the ticket(s)
        for( int counter = 1; counter <= numberOfTickets; ++counter)
            System.out.printf( "LOTTO TICKET %d:\n", counter );
            System.out.print( getLottoTicket() );
        System.out.print( "\n\nProgrammed by Jolie O'Dell.\n\n" );

If this helps you at all, I’m ever so glad.

Also, big freakin’ ups to programmer Greg Houston for creating this lovely code-to-HTML app. Dude, I can’t tell you how much I love you for this. It’ll make blogging about code — both here and probably on Mashable — a hell of a lot easier.

Image courtesy of journeyscoffee.

8 thoughts on “Java 101: SuperLotto

  1. I don’t know about your assignment, but are you allowed to have repeated number in your ticket? This could happen with your code.

  2. Nice work. You may want to replace the numbers with constants like this though:

    private static final int MAX_RANDOM_NUMBER = 47;
    private static final int MAX_MEGA_RANDOM_NUMBER = 27;

    There are style concerns with having “magic numbers” in the body of a program, because they can be a hassle to change if they’re repeated through the code. If you’re using an IDE like Eclipse or Intellij IDEA, you can use the refactoring options to extract the constant for you.

  3. Really takes me back. Specifically to Java, that is. I took a 20-week semester class in 2006. They had a 2nd class, but it wasn’t mandatory. Used the same book, though. Whopper of a book. I should revisit the first half and then finish it.


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s