Unique values

Posted on 2006-05-18
Last Modified: 2010-03-31

I have 3 dropdownlist in my Swing GUI which has values of 1-10 each, how do i ensure
the user does not select a repeated number on submit

Question by:jedistar
    LVL 24

    Expert Comment

    You could disable already used numbers, but 10 is quickly exhausted.
    What is your purpose?

    LVL 14

    Expert Comment

    you mean the user should not select 3 same numbers ?
    simply comare the values on submit and warn him if two are the same
    LVL 24

    Accepted Solution

    You might use:

            String s = (String)JOptionPane.showInputDialog( ....)

    You feed this dialog with a String[] which only contains the numbers that you can still choose from.

    Of course the returned String should be converted to int if necessary.


          String[] numberList = new String[]{"1", "2", "3", "4", "5","6", "7", "8", "9", "10"};
          String s = (String)JOptionPane.showInputDialog
             "Choose a number",
             null                                // Default, unwanted now
    After choice made, delete the choice from 'numberList' and go on.


    Author Comment

    yeah for example. they can select

    1 2 3

    but not

    1 1 2 or 1 2 2

    whats the most efficient algo to compare and return false if same nums detected
    LVL 14

    Expert Comment

    > most efficient
    since they are only 3

    if(v1 == v2 || v2 == v3 || v1 == v3)
        repeated number detected

    Author Comment

    if there are 20?
    LVL 14

    Expert Comment

    you can follow sciuriware's suggestion
    - that is removing any selected value from the list of available values, forcing the user to not select any preselected value

    to see whether there are any repeated numbers in an array
    linear search is acceptable for 20 items
    but for larger amounts (more than 1000), I suggest you use hashtable or binary search tree, which are faster in for this purpose
    LVL 30

    Expert Comment

    >> if there are 20?

    Add them to an array or an array-list and loop through (you can also sort them and use binarySearch () from java.util)
    LVL 4

    Expert Comment

    I think the best way to do it is to ensure that when a user selected an item in the list, the other lists update.  This is accomplished by adding an ItemListener action handler to each combo box.  The action handler for the first box would just set the second / third one such that the selected item wasn't in it, etc.  And you'd do the same for the second control as well (it would just update the third control).

    Maybe a combo box for selecting a number is the wrong interface component?  Maybe a JSpinner control would be easier to manage?

    If you want to check for duplicates then why not create a Set (where objects aren't repeated), if the size of the Set is equal to the number of selections, then there are no duplications, otherwise there are.

    java.util.Set<Integer> set = new java.util.HashSet<Integer>()

    set.add( 1 );
    set.add( 2 );
    set.add( 3 );
    set.add( 4 );
    set.add( 4 );

    if ( set.size() < 5 )
      System.out.println( "Duplicates detected" );

    This is obviously J2SE 5 code (because of the generics and auto-boxing), but the same ideas apply for older versions of Java.
    LVL 24

    Expert Comment


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Which XML parser should I used for my requirement 11 49
    scoreUp challenge 14 40
    Receive file in Servlet 1 28
    parentbit challenge 3 37
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now