Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Solving the error: return true if the string "cat" and "dog" appear the same number of times in the given string.
Next
Answered
Ari Johnson
  • 13
Ari Johnson
Asked: May 18, 20222022-05-18T17:37:47+00:00 2022-05-18T17:37:47+00:00In: java

Solving the error: return true if the string “cat” and “dog” appear the same number of times in the given string.

  • 13

. Advertisement .

..3..

. Advertisement .

..4..

Here is the program I run:

public boolean catDog(String str) 
 { 
  int catAnswer = 0;
  int dogAnswer = 0;
  int cat_Count = 0;
  int dog_Count = 0;
 
  for (int i=0; i< str.length()-1; i++) 
  {
  String sub = str.substring(i, i+2);
 
 
  if ((sub.equals("cat"))) cat_Count++;
  if ((sub.equals("dog"))) dog_Count++;
  catAnswer = cat_Count; 
  dogAnswer = dog_Count;
 
  } //end for
 
  if(dogAnswer == catAnswer ) {return true;}
  // else
  return (dogAnswer != catAnswer);
 }

After I run, it returns an error:

Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 7 (line number:10) - hence i use i + 2 (no errors are reported with it

Does anyone have any suggestions for the problem below: return true if the string “cat” and “dog” appear the same number of times in the given string in the java – How to correct it?

string
  • 2 2 Answers
  • 75 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-21T10:21:25+00:00Added an answer on June 21, 2022 at 10:21 am

    The cause: Run time exception is to blame for the error. StringIndexOutOfBoundsException is issued because you are accessing the element at a position that is bigger than its length.

    Solution: Using split is “all correct” answer for the error: return true if the string “cat” and “dog” appear the same number of times in the given string. To retain trailing empty strings, use the -1.

    int count(String needle, String haystack) {
    return haystack.split(needle, -1).length - 1;
    }
    
    public boolean catDog(String str) {
    return count("dog", str) == count("cat", str);
    }
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Enoch Evans
    2022-05-26T04:10:25+00:00Added an answer on May 26, 2022 at 4:10 am

    Although you’re close to finding a solution, there are some critical issues in your code.

    1. A string of 2 is returned by substring() when you call substring(). This string cannot ever be cat or dog. To get 3 characters, change the second parameter of the method call i + 3.

    2. Calling substring() will result in IndexOutOfRangeException as i nears the end of the input string. You should not ask for substrings that exceed the length of your input string. This bug can be fixed by changing the for conditional expression to i < str.length() - 2.

    3. Your method’s return value will always be true. If dogAnswer != catAnswer is used, you will return the exact same expression. This will resolve to true. One solution is to merge the return statements into return dogAnswer == catAnswer.

    There are also a few things that you can do to make your code easier:

    1. It is not necessary to convert cat_Count into catAnswer or dog_Count into dogAnswer. You can discard two variables and only use one pair.


    2. Your loop can only be optimized to consider the third position of the input string if it is restricted to cat or dog. Change i++ into i += 3.

      (Update: After reviewing the test data at

      CodingBat

      (I can assure you that this isn’t the case.

    After I had implemented fix #1, #2, and #3, as well as suggestion 1, I ran a test using the test bench. The result was quite satisfying.


    return%20true%20if%20the%20string%20%22cat%22%20and%20%22dog%22%20appear%20the%20same%20number%20of%20times%20in%20the%20given%20string.

    All right

    • 24
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.