Results 1 to 6 of 6
  1. #1
    carlasofb is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Question Validate an array????

    Hi, I'm starting to learn JAVA and they asked me for a method that checks if an array of 9 positions contains the numbers 1-9 (the order doesn't matter).

    Plz help!!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

  3. #3
    carlasofb is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Validate an array????

    public static boolean contains1to9(int[] v) {

    boolean check = true;
    int count = 0;

    for (int j=0; j<v.length-1; j++) {
    if (v[j] == v[j+1]) {
    count++;
    check = false;
    }

    if (count == 0 && v[j] > 0 && v[j] < 10)
    check = true;
    }

    return check;

    }

  4. #4
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Validate an array????

    Let me get this straight, the array must be of size 9 and contain the integers 1-9 in any order? ex:

    v = { 1,2,3,4,5,6,7,8,9 } or
    v = { 1,9,2,8,3,7,4,6,5 } or
    v = { 9,1,6,7,8,3,2,5,4 } or
    ...
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  5. #5
    carlasofb is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Validate an array????

    Yes, that's it. I think my code works now, I've done a few changes to it. It may not be the best but it does what's supposed to!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: Validate an array????

    Stick them all in a SortedSet and afterwards check if the first (lowest) element is a 1, the last (highest) element is a 9 and that the Set contains nine elements.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. wont validate
    By karq in forum XML
    Replies: 1
    Last Post: 10-07-2010, 05:33 PM
  2. can someone validate my code?
    By alacn in forum New To Java
    Replies: 6
    Last Post: 08-08-2010, 03:18 AM
  3. Validate url in a JTextField
    By knickerbocker in forum New To Java
    Replies: 16
    Last Post: 05-30-2010, 03:09 PM
  4. validate between int and double
    By heartysnowy in forum New To Java
    Replies: 4
    Last Post: 10-05-2009, 07:15 PM
  5. validate hex character
    By paul in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:08 PM

Tags for this Thread

Posting Permissions

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