Results 1 to 13 of 13

Thread: Char[] issue

  1. #1
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Question char[] issue

    Hi Guys

    Can someone please tell me how I can check if a Char array ( Char[] ) has a certain character !

    I did ( .contains(char) ) but it says an Array doesn't have the method .contains !

    I'm stuck. I would greatly appreciate any help.

    Thank You.
    Last edited by Ciwan; 05-21-2011 at 11:22 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Look at the Arrays class. It is full of utilities for working with arrays.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Char[] ? Do you mean char[]? Or Character[]? If you use an Character[]-array you could use Arrays.asList(yourArray).contains(yourChar) !
    If you use char[] you could iterate over the array by yourself. Or you sort the array with Arrays.sort and use the method binarySearch :D
    Last edited by eRaaaa; 05-21-2011 at 10:59 PM.

  4. #4
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Sorry I meant to write char[].

    I have done the following:

    Java Code:
    /**
         * The function below would get a node that isn't in the current proposedPath.
         * @return String node
         */
        public String getNextSuitableNode()
        {
            char[] cArray = proposedPath.toCharArray(); // converting the proposedPath string into an array of characters.
            String nextNode = getNeighboursNames().get(new Random().nextInt(getNeighboursNames().size()));
            if(!cArray.asList(cArray).contains(nextNode.charAt(0))) // i.e. if the Char Array doesn't contain that nextNode.
            {
                return nextNode;
            } else {
                // create another random node and try again.
            }
        }
    But BlueJ complains and says that the method asList() doesn't exist !! :(

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    First of all, do you mean char[] or Character[]?

    Either way, you could iterate over it and compare each element to the one you're looking for. Are you familiar with 'for' statements?

    Alternatively, if it's a char[] and you want to be lazy, just construct a String from it and use String's indexOf method.
    Get in the habit of using standard Java naming conventions!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    If your are unable to read the API doc for the Arrays class, here is a sample usage:

    char[] chars = {'a', 'b', 'c', 'd'};
    System.out.println("res=" + Arrays.binarySearch(chars, 0, chars.length, 'd')); //res=1 for b, 3 for d

  7. #7
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Hmm I just pasted my code above. I think it is pretty clear that I meant char[]. :) If not clear, I apologise.

    @Norm: I understand how an Array and its indexes work. But I couldn't get my head around why an Array object wouldn't have a ( .contains() ) method. I know I can go through each object in the Char array and see if it equal my desired character. But I thought there might be a simpler, neater way. like asList() < which unfortunately doesn't work in this case.

  8. #8
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Here's what I've come up with:

    Java Code:
    /**
         * The function below would get a node that isn't in the current proposedPath.
         * @return String node
         */
        public String getNextSuitableNode()
        {
            char[] cArray = proposedPath.toCharArray(); // converting the proposedPath string into an array of characters.
            String nextNode = getNeighboursNames().get(new Random().nextInt(getNeighboursNames().size())); // Grabbing a random String from the ArrayList<String> returned from the method getNeighboursNames().
            for(char c : cArray) // for-each char in the char array
            {
                if(!(c == nextNode.charAt(0))) // check that it doesn't equal the nextNode string. which really consists of just 1 character. 
                {
                    return nextNode;
                }
            }
            return getNextSuitableNode();
        }
    I'm not too good with for-each loops. Can someone please check the logic. Here is what I want this method to do:

    Check if nextNode (which is a String of length 1 char) exists in the char array. If it does, get another (but different) randomly generated nextNode. If on the other hand it doesn't exist in the char array. Then that nextNode is perfect and we want to return it.

    Does my above code do that ?

    Thanks

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    how I can check if a char array ( char[] ) has a certain character
    That is EXACTLY what the Arrays method I posted does.

    What is it you want to do? Can you explain and give an example?


    Does my above code do that ?
    Write a simple program to test your code. Add lots of printlns to see what the code does.
    Give the simple program lots of different input and see what happens.
    Last edited by Norm; 05-21-2011 at 11:52 PM.

  10. #10
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Sorry, I'm in the habit of opening a bunch of tabs and not refreshing before I post my replies.
    Get in the habit of using standard Java naming conventions!

  11. #11
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Hehe it's OK Kjkrum. :)

    @Norm. Thank you. Your suggestion about trying it with lots of System.out.println statements worked.

    Thank You all.

  12. #12
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Use an enhanced for loop.

    Java Code:
    char[] characters = new char[3];
    characters[0]='f';
    characters[1]='d';
    characters[2]='c';
    
    for(char c : characters) {
    if(c=='d') {
    System.out.println("Contains the letter d!");
    }
    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    The Arrays class method does that. You don't have to code a loop.

Similar Threads

  1. Char Count Issue
    By Goldie in forum New To Java
    Replies: 3
    Last Post: 03-28-2011, 12:02 AM
  2. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  3. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  5. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 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
  •