Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    [Resolved] Simple javacode

    Im a beginner at java and I've been assigned this exercise I've been working on it for a while now but I just cant seem to get it right.
    Return true if the string "cat" and "dog" appear the same number of times in the given string.
    this is my code so far.
    public boolean catDog(String str)
    {
    int count = 0;
    for (int i=0; i<str.length(); i++)
    {
    String sub = str.substring(i, i+1);
    if (sub.equals("cat"))
    count++;
    if (sub.equals("dog"))
    count--;
    }

    {return false;}

    }

    any hints on how to fix it?

    http://www.javabat.com/prob?id=String2.catDog
    Last edited by karla00; 10-26-2008 at 09:22 PM.

  • #2
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Never mind all I had to do was assigned different counter variables lol thanks for your hep guys!!


  •  

    Posting Permissions

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