DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2002

    Post Removeing duplicate numbers in Command Prompt

    I am learning the fundamentals of the Java language in the command prompt window. Hopefully a few of you people will know how to do this.

    I have made a program that calculates random numbers for lottery tickets. What I need to do is remove any duplicates that show up in the output. Can someone tell me how I should go about this? (WARNING! This code is a little messy. I still have to polish it up. )

    public class Super7 {

    public static void main(String [] args) {

    //Declare the variables
    final int TICKET_PRICE = 2;
    int [] boardNumber = new int [7];
    int customerTickets;
    int maxValue;
    int maxIndex;
    int customerPrice;
    //Ask the user how many tickets he wishes to buy
    System.out.print("How many tickets would you like to buy @ $2.00/each: ");
    customerTickets = MyInput.readInt();

    //Generate the numbers so that each ticket get 3 boards of 7 numbers from 1-49, depending on how many

    //tickets he wishes to buy
    for (int tickets = 0; tickets < customerTickets; tickets++) {
    System.out.println("\n\t The numbers for your Super 7 ticket(s) are: ");
    //populate the boardNumber array
    for (int numberOfBoards = 0; numberOfBoards < 3; numberOfBoards ++) {
    for (int board = 0; board < boardNumber.length; board++) {
    boardNumber[board] = (int) (Math.random() * 49 + 1);
    }//end for

    for(int outerIndex = boardNumber.length-1; outerIndex>=1; outerIndex--) {
    maxValue = boardNumber [outerIndex];
    maxIndex = outerIndex;

    for (int innerIndex = outerIndex - 1; innerIndex>=0; innerIndex--) {
    if(maxValue < boardNumber[innerIndex]) {
    maxValue = boardNumber[innerIndex];
    maxIndex = innerIndex;
    }//end if
    }//end for

    if (maxIndex != outerIndex) {
    boardNumber[maxIndex] = boardNumber[outerIndex];
    boardNumber[outerIndex] = maxValue;

    }//end if
    }//end for

    for (int i = 0; i < boardNumber.length; i ++) {
    System.out.print("\t" + boardNumber[i]);
    }//end for
    }//end for
    }//end for
    customerPrice = customerTickets * TICKET_PRICE;
    System.out.println("\n Your cost for " + customerTickets + " ticket(s) is $" + customerPrice + ".00");
    }//end main

    }//end class

    The MyInput.readInt() just reads the users input, it is what we have been given to get the users input.

    Thanks for your help, and takeing the time to go through that code, it ain't pretty

  2. #2
    Join Date
    Sep 2002
    for (int board = 0; board < boardNumber.length; board++)
       while (boolean unique == false)
          boardNumber[board] = (int) (Math.random() * 49 + 1);
          for (int count=0; count<board; count++)
             if (boardNumber[board] == boardNumber[count])
                count = board; //exits for loop and gets another number
                if (count == (board-1))  //i know this if statement shouldn't be placed like this but i want it to look simple :)
                   unique = true;
    hope this helps, i havn't compiled it but i think it should work :D good luck
    ¿Red Baron?
    [code] /* dont forget code tags! */ [/code]
    Come visit the Café!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.