LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 476 users online 157503 members 2372 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Video | Dictionary | News | FAQ
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
4 online / 29 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic

Java - For Loops
Driving me crazy...
Replies: 3Last Post May 1 12:05pm by DarkLink224
Single page for this topic Email Print Favorite
( xren )


Connoisseur

Ad Free
Reply
Can anyone help me with this simple for loop, that just isn't working for me? If you would like to use the website that I'm doing this on, it is on javabat.com, String2 -> catDog.

But for those who would rather not visit the site, please look at my code and tell me what I'm missing, because it really is driving me crazy.

Problem: Return true if the string "cat" and "dog" appear the same number of times in the given string.

My Code:


public boolean catDog(String str)
{

boolean result = false;
int cat = 0;
int dog = 0;
int len = str.length();

for(int i = 0; i <(len-2);i++)
{

 String sub = str.substring(i, i+3);
 if (sub.equals("cat")) cat ++;
 if (sub.equals("dog")) dog ++;

 if (cat != dog) result = false;
 else result =  true;
}
return result;

}

}

Any (useful) help is appreciated =)

Post edited at 12:38 pm on April 3, 2008 by xren

-------
N&R;D
you&me babe
October 10, 2004


12:11 pm on April 3, 2008 | Joined Dec. 2003 | 380 Days Active
Join to learn more about xren California, United States | Label Free Female | 409 Posts | 6579 Points
Whuppee


Dairy Product Addict

Ad Free
Reply
Why it doesn't work:

    for(int i= 0; i < (len-2); i++)

      An i of 0 is not less than a length of (less than or equal to) 2 minus 2.

      Result is initialized to false, and only modified within the loop.

      Since the loop will never run for lengths less than or equal to 2, the result can only be false.


    For a simple fix, consider..

    If the purpose of the loop is to count how many times both "dog" and "cat" occur..

    Is there any point to knowing if they've occured the same amount of times, or not, when you're not done counting?


Misc. Efficiency:
  • Declare String sub; before / outside of the loop.

    The optimizer should resolve this for you, but it still shouldn't be (re)declared every pass through the loop.

  • Your two ifs should be an if/else if.

    If the string is "cat", it can't also be "dog".

  • As above, determine if cat == dog after / outside of the loop.

Post edited at 4:18 pm on April 4, 2008 by Whuppee

-------
Unbelievably awesomesauce to the absurd degree that I wet myself.
Who else has created a LW/magnets
so explicit the mods deleted a screencap of it? =P


4:16 pm on April 4, 2008 | Joined May 2002 | 336 Days Active
Join to learn more about Whuppee Oregon, United States | Lesbian Male | 1489 Posts | 6609 Points
( xren )


Connoisseur

Ad Free
Reply
Thanks a lot for the help.

It makes a lot more sense, I just always miss the little details every now and then and then I go crazy.

I appreciate the help. It works btw.

-------
N&R;D
you&me babe
October 10, 2004


10:18 pm on April 7, 2008 | Joined Dec. 2003 | 380 Days Active
Join to learn more about xren California, United States | Label Free Female | 409 Posts | 6579 Points
DarkLink224


[A blackened chain part]

Patron
Reply
My CS teacher makes us do these quizzes too but when I did that particular one, I found it easier to use the indexOf method with the second fromIndex parameter and just keep narrowing the string.  And instead of keeping track of the numbers of cats and dogs, I used a single counter and incremented it with every cat occurence, and decremented with every dog occurence.

That's all a matter of taste though.  


12:05 pm on May 1, 2008 | Joined April 2004 | 1069 Days Active
Join to learn more about DarkLink224 Virginia, United States | Straight Male | 15416 Posts | 25833 Points
Single page for this topic Email Print Favorite

Quick Reply

You are signed in as our guest.

Looking for something else?
 

  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic