[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Unique values

Hi,

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

thanks.
0
jedistar
Asked:
jedistar
  • 3
  • 3
  • 2
  • +2
1 Solution
 
sciuriwareCommented:
You could disable already used numbers, but 10 is quickly exhausted.
What is your purpose?

;JOOP!
0
 
hoomanvCommented:
you mean the user should not select 3 same numbers ?
simply comare the values on submit and warn him if two are the same
0
 
sciuriwareCommented:
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.

E.g.:

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

;JOOP!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jedistarAuthor Commented:
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
0
 
hoomanvCommented:
> most efficient
since they are only 3

if(v1 == v2 || v2 == v3 || v1 == v3)
    repeated number detected
0
 
jedistarAuthor Commented:
if there are 20?
0
 
hoomanvCommented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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)
0
 
fffej78Commented:
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.
0
 
sciuriwareCommented:
:))
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now