Results 1 to 7 of 7
  1. #1
    ricky is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default help with function in java!!!!

    /**
    * Write the function catDog.
    *
    * @param str the input string to examine.
    * @return true if "cat" and "dog" appear the same number of times.
    *
    * Return true if the string "cat" and "dog" appear
    * the same number of times in the given string.
    *
    * Some examples:
    * catDog("catdog") returns true
    * catDog("catcat") returns false
    * catDog("1cat1cadodog") returns true
    */
    This is my first time playing with the loop method so im kinda noob. please help me with this.

    public boolean catDog(String str)
    {
    boolean result = true;
    int cat = 0;
    int len = str.length() - 2;
    for (int i = 0; i < len; i ++)
    {
    String word = str.substring(i, i + 3);
    if (word.equals("cat"))
    cat++;

    }

    return result;
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have a string and looking some text matching in your code with the index, and do some process based on that. Then whatever you found return true. So what you exactly want to do?

  4. #4
    suhassr is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    u can use the str.contains() function to check if a sequence exists in the string str.

    Increment a counter if "cat" is encountered and decrement it if "dog" if encountered.
    The counter value will be zero if "cat" & "dog" are present equal no. of times.

  5. #5
    suhassr is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    u can use the str.contains() function to check if a sequence exists in the string str.

    Increment a counter if "cat" is encountered and decrement it if "dog" if encountered.
    The counter value will be zero if "cat" & "dog" are present equal no. of times.

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Or use regex :)
    I die a little on the inside...
    Every time I get shot.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Calling Java Function through JSP
    By Vikrant Pawar in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-06-2008, 06:28 AM
  2. How to use a function in java
    By olikhvar in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 02:57 AM
  3. Use Perl function with java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 AM
  4. Help with recursive function in java
    By cachi in forum Advanced Java
    Replies: 2
    Last Post: 07-31-2007, 06:51 PM
  5. Php explode function, equivalent java
    By fernando in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:44 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
  •