Thread: Check if value exists in array

Results 1 to 4 of 4
  1. #1 Check if value exists in array 
    Registered Member oleaa2's Avatar
    Join Date
    Feb 2011
    Posts
    51
    Thanks given
    9
    Thanks received
    3
    Rep Power
    0
    Hello guys,

    I'm working on a jewellery amulet/ring teleport system and i need some help

    i have declared a array in server.Config like this:

    Code:
    	
    public static final int[] JEWELLERY	=	{1712,1710,1708,1706}; //items that has a jewellery function
    made a file called Jewellery.java in server.model.players

    i have a function called isJewellery (public boolean isJewellery(int itemId))

    Code:
    	public boolean isJewellery(int itemId) {
    		return Config.JEWELLERY.contains(itemId);
    	}
    ^in the function over (isJewellery) im trying to check if the array 'Config.JEWELLERY' contains '(int) itemId' and want it to return true or false, but its not working.
    Spoiler for Compiling error:

    src\server\model\players\Jewellery.java:35: cannot find symbol
    symbol : method contains(int)
    location: class int[]
    return Config.JEWELLERY.contains(itemId);
    ____________________^


    so my question is:
    Is there a way to check if a array contains a specific value (integer) and return true if it does and false if it not.

    Thanks, i will rep+ the user that can help me

    Cheers,
    Oleaa
    Reply With Quote  
     

  2. #2  
    Registered Member
    Lil Str Kid's Avatar
    Join Date
    Jul 2007
    Age
    31
    Posts
    1,302
    Thanks given
    169
    Thanks received
    71
    Rep Power
    260
    Code:
    public boolean isJewellery(int itemId) {
          for (int i = 0; i < Config.JEWELLERY.length; i++) {
             if (Config.JEWELLERY[i] == itemId) {
               return true;
          }
          return false;
    }


    Reply With Quote  
     

  3. #3  
    Registered Member
    Whired's Avatar
    Join Date
    Aug 2007
    Posts
    2,126
    Thanks given
    238
    Thanks received
    500
    Rep Power
    822
    contains is for a collection, you have a primitive array

    you need to manually test each value

    Code:
    	public boolean isJewellery(int itemId) {
    		for(int j : Config.JEWELLERY) {		// Loop through all (enhanced for loop/for-each)
    			if(j == itemId) {
    				return true;				// Match found, return true
    			}
    		}
    		return false;						// No matches, return false
    	}
    Reply With Quote  
     

  4. #4  
    Registered Member oleaa2's Avatar
    Join Date
    Feb 2011
    Posts
    51
    Thanks given
    9
    Thanks received
    3
    Rep Power
    0
    Thanks Lil Str Kid and Whired. it worked
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Directory Exists
    By Queer in forum Help
    Replies: 6
    Last Post: 02-25-2012, 05:15 AM
  2. Nothing Exists??????????
    By Owner of Flamescape in forum Help
    Replies: 8
    Last Post: 06-25-2010, 07:30 AM
  3. IF row exists?
    By Tzar in forum Application Development
    Replies: 7
    Last Post: 02-03-2010, 01:59 AM
  4. Check if an object exists
    By FuglyNerd in forum Help
    Replies: 2
    Last Post: 11-30-2009, 06:49 PM
  5. Replies: 4
    Last Post: 08-26-2009, 07:14 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •