Results 1 to 7 of 7
  1. #1
    IYIaster is offline Member
    Join Date
    Jul 2009
    Posts
    46
    Rep Power
    0

    Default hope there is an easier way.

    What I'm trying to do is I have 10 textfields and I want to be able to compare all 10 of them to make sure that none of them have the same info typed in. I know I can go thru miles of code to compare them all but that will take a long time. I'm sure there is an easier way. I'm just not sure how. Any ideas?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    By using a loop?

    e.g.

    Java Code:
    String[] a = { "a", "b", "c", "d", "c" };
    for (int i = 0; i < a.length; i++) {
      for (int j = i + 1; j < a.length; j++) {
        if (a[i].equals(a[j])) {
          // error two strings are the same
        }
      }
    }
    and read the contents of the textfields into an array, or use an array of the textfields themselves using getText by each side of the comparison.
    Last edited by masijade; 10-13-2009 at 06:03 PM.

  3. #3
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Java Code:
    Set<String> countMe = new TreeSet<String>();
    ...
    //put each textfield's value into the set
    set.add(textfield1.getText());
    ...
    if(countMe.size()!=10)
    {
       System.out.println("Hey, not unique!");
       ...
    }
    ...
    My Hobby Project: LegacyClone

  4. #4
    IYIaster is offline Member
    Join Date
    Jul 2009
    Posts
    46
    Rep Power
    0

    Default

    This wasn't working at first. But I figured out why. It would through an error everytime. You see the 10 textfields are not always visible is depends on something that is chosen. So, I'm guessing that if you only choose to show 3 of the textfields then the other 7 have the samething inside (nothing) Is there an easy way of getting around this?

    This is what I've done
    Java Code:
                String[] a = { ip2, ip3, ip4, ip5, ip6, ip7, ip8, ip9, ip10 };
    for (int i = 0; i < a.length; i++) {
      for (int j = i + 1; j < a.length; j++) {
        if (a[i].equals(a[j])) {
    error1 = 1;
        }
      }
    }

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    if ((!a[i].equals("")) && (!a[j].equals("")) && (a[i].equals(a[j]))) {

  6. #6
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Java Code:
    if ((!a[i].equals("")) && (!a[j].equals("")) && (a[i].equals(a[j]))) {
    Is the middle predicate necessary? if a[i] != "" and a[i] == a[j] then
    is it possible for a[j] == "" ?

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    No, it's not necessary, but it does make the intention clear. The extra few nanoseconds it takes to check that is worth it to make the intention clear to any future programmer who may need to maintain the code (not that I think that is the case here, but hey).

    Edit:
    But, I would probably simply check before entering the second loop.
    Java Code:
    for (int i = 0; i < a.length; i++) {
      if (a[i].equals("")) continue;
      for (int j = i + 1; j < a.length; j++) {
        if (a[i].equals(a[j])) {
          error1 = 1;
        }
      }
    }
    Last edited by masijade; 10-14-2009 at 07:29 AM.

Similar Threads

  1. A good Java installation package can make things much easier
    By freezea in forum Reviews / Advertising
    Replies: 2
    Last Post: 02-01-2009, 12:03 PM
  2. what about thinkink in easier way for making tables
    By soharto in forum AWT / Swing
    Replies: 3
    Last Post: 09-09-2008, 12:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •