Hey guys,
I need your help. I need to write a Java program for a simulation for a lottery. I got so far as I got numbers and everything. But how can i make sure that there is no number going to be there twice?
Thanks for your help
Julie

Could you use an Else If type thing.

put all lottos numbers in an array and search through it....
use a do-while loop

make sure to initialize your array to something that would be invalidlike 0 to avoid a null pointer exception

it might help if we could see your code...

This is from the Deitel Book. I also post the requirements they need. And to answer your question, look at number 6. ;)

1. need to add for a "for" statement that will execute the application 4 times.
2. Using a 2 dimensional boolean array (uniqueNumber) . THe value of that variable in the array should be set to false. Add a nested "for" statement to set the 40 values to 'false'. I think it requires a for statement to index the array.
3. Init a String to hold selections.
4. Iterating over 5 numbers selected. Each time this statement executes, a single unique lottery value will be generated.
5. a do...while statment. Using a method "generate" pass it to args 0-39. To return a random number.
6. Add code to set the value in uniqueNumber represented by the selected number to true. It won't be selected again.
7. then it's supposed to display.

// This application picks randomly generated numbers for a lottery.
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.*;

public class LotteryPicker extends JFrame
{
   // JLabel and JTextField for first lottery
   private JLabel oneJLabel;
   private JTextField oneJTextField;

   // JLabel and JTextField for second lottery
   private JLabel twoJLabel;
   private JTextField twoJTextField;

   // JLabel and JTextField for third lottery
   private JLabel threeJLabel;
   private JTextField threeJTextField;

   // JLabel and JTextField for fourth lottery
   private JLabel fourJLabel;
   private JTextField fourJTextField;

   // JButton to generate lottery numbers
   private JButton generateJButton;

   // Random object to create random integers
   private Random generator = new Random();

   // two-dimensional array to maintain unique random numbers
   private boolean[][] uniqueNumber = new boolean[ 4 ][ 40 ];

   // one-dimensional array to store strings for output
   private String[] output = new String[ 4 ];

   // instance variable to hold the selected lottery number
   private int selection;
   
   // no-argument constructor
   public LotteryPicker()
   {
      createUserInterface();
   }
   
   // create and position GUI components; register event handlers
   private void createUserInterface()
   {
      // get content pane for attaching GUI components
      Container contentPane = getContentPane();

      // enable explicit positioning of GUI components
      contentPane.setLayout( null );

      // set up oneJLabel
      oneJLabel = new JLabel();
      oneJLabel.setBounds( 16, 18, 100, 16 );
      oneJLabel.setText( "First lottery:" );
      contentPane.add( oneJLabel );

      // set up oneJTextField
      oneJTextField = new JTextField();
      oneJTextField.setBounds( 120, 16, 124, 23 );
      oneJTextField.setHorizontalAlignment( JTextField.CENTER );
      oneJTextField.setEditable( false );
      contentPane.add( oneJTextField );

      // set up twoJLabel
      twoJLabel = new JLabel();
      twoJLabel.setBounds( 16, 50, 100, 16 );
      twoJLabel.setText( "Second lottery:" );
      contentPane.add( twoJLabel );

      // set up twoJTextField
      twoJTextField = new JTextField();
      twoJTextField.setBounds( 120, 48, 124, 23 );
      twoJTextField.setHorizontalAlignment( JTextField.CENTER );
      twoJTextField.setEditable( false );
      contentPane.add( twoJTextField );

      // set up threeJLabel
      threeJLabel = new JLabel();
      threeJLabel.setBounds( 16, 82, 100, 16 );
      threeJLabel.setText( "Third lottery:" );
      contentPane.add( threeJLabel );

      // set up threeJTextField
      threeJTextField = new JTextField();
      threeJTextField.setBounds( 120, 80, 124, 23 );
      threeJTextField.setHorizontalAlignment( JTextField.CENTER );
      threeJTextField.setEditable( false );
      contentPane.add( threeJTextField );

      // set up fourJLabel
      fourJLabel = new JLabel();
      fourJLabel.setBounds( 16, 114, 100, 16 );
      fourJLabel.setText( "Fourth lottery:" );
      contentPane.add( fourJLabel );

      // set up fourJTextField
      fourJTextField = new JTextField();
      fourJTextField.setBounds( 120, 112, 124, 23 );
      fourJTextField.setHorizontalAlignment( JTextField.CENTER );
      fourJTextField.setEditable( false );
      contentPane.add( fourJTextField );

      // set up generateJButton
      generateJButton = new JButton();
      generateJButton.setBounds( 148, 150, 96, 24 );
      generateJButton.setText( "Generate" );
      contentPane.add( generateJButton );
      generateJButton.addActionListener( 
      
         new ActionListener() // anonymous inner class
         {
            // event handler called when generateJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               generateJButtonActionPerformed( event );
            }
            
         } // end anonymous inner class
         
      ); // end call to addActionListener

      // set properties of application's window
      setTitle( "Lottery Picker" ); // set title bar string
      setSize( 264, 212 );          // set window size
      setVisible( true );           // display window
      
   } // end method createUserInterface

   // generate four random five-number lottery combinations
   private void generateJButtonActionPerformed( ActionEvent event )
   {
     (HELP HERE)   
 
   } // end method generateJButtonActionPerformed
   
   // generate random number in a given range
   private int generate( int low, int high )
   {
      // generate random number in range from low to high
      return low + generator.nextInt( high - low + 1 );

   } // end method generate
   
   // main method
   public static void main( String[] args )
   {
      LotteryPicker application = new LotteryPicker();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } // end method main
   
} // end class LotteryPicker

/**************************************************************************
 * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************/

Code tags added. -Narue

There are basically two ways to ensure that no number is repeated. The first way is to randomly permute a list of numbers with no duplicates. Then you can just ask for n numbers from that list and they should be fairly random:

public class scratch {
  public static void main(String[] args)
  {
    new lottery ( 20 );
  }
}

class lottery {
  private int[] numbers;

  public lottery ( int n )
  {
    numbers = new int[n];

    // Initialize lottery numbers
    for ( int i = 0; i < n; i++ ) {
      numbers[i] = i;
    }

    show_all ( numbers );
    random_shuffle ( numbers );
    show_all ( numbers );
  }

  private void random_shuffle ( int[] list )
  {
    for ( int i = 0; i < list.length - 1; i++ ) {
      int r = (int)( Math.random() * ( list.length - i ) );
      int save = list[i];
      list[i] = list[i + r];
      list[i + r] = save;
    }
  }

  private void show_all ( int[] list )
  {
    for ( int i = 0; i < list.length; i++ ) {
      System.out.print ( list[i] + " " );
    }
    System.out.println();
  }
}

The second way is to build a list straight from the random number generator and check which numbers you've already inserted to avoid inserting it again. There's a large number of ways you can go about this, the simplest being a search in the existing array for each new number:

public class scratch {
  public static void main(String[] args)
  {
    new lottery ( 20 );
  }
}

class lottery {
  private int[] numbers;

  public lottery ( int n )
  {
    numbers = new int[n];

    // Initialize lottery numbers
    int i = 0;

    while ( i < n ) {
      int r = (int)( Math.random() * n );

      if ( add ( numbers, i, r ) ) {
        ++i;
      }
    }

    show_all ( numbers );
  }

  private boolean add ( int[] list, int size, int val )
  {
    for ( int i = 0; i < size; i++ ) {
      if ( list[i] == val ) {
        return false;
      }
    }

    list[size] = val;

    return true;
  }

  private void show_all ( int[] list )
  {
    for ( int i = 0; i < list.length; i++ ) {
      System.out.print ( list[i] + " " );
    }
    System.out.println();
  }
}
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.