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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8032651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:27:03+00:00 2026-06-05T01:27:03+00:00

I am doing a problem from codingbat and I am stuck at this problem.

  • 0

I am doing a problem from codingbat and I am stuck at this problem. The question asks me to find all the 'hi' in the string but ignore the 'hi' which have 'x' just before them. In other words, don’t count 'xhi' but only 'hi'.

Every input works fine in my code except for when the input is "xxxx".
My code is as follows:

public int countHi2(String str) {
      String s = "hi";
      int count = 0;

      if(str.length() < 2) {
          return 0;
      }
      else if(str.charAt(0) == 'x' && str.substring(1,3).equals(s)) {
           count+= countHi2(str.substring(3));
      }
      else if(str.substring(0,2).equals(s)){
          count+= 1 + countHi2(str.substring(2));
      }
      else {
          count+= countHi2(str.substring(1));
      }
      return count;
}

The problem is that it throws IndexOutOfBoundsException. The link to the question can be found here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:27:04+00:00Added an answer on June 5, 2026 at 1:27 am

    You see an exception because substring throws IndexOutOfBoundsException if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex. You check for the length to be at least 2, and then do substring(1, 3), causing an exception.

    You can avoid the problem with IndexOutOfBoundsException in substring by switching to startsWith API, which does not throw exceptions even when you compare your string to a longer one.

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

Sidebar

Related Questions

While doing some small regex task I came upon this problem. I have a
Im doing a page in .asp and im having this problem ... I have
I got this problem: invalid conversion from ‘void*’ to ‘uint8_t*’ When doing this: int
I am doing a transformation from a customoized set cover problem to a sat,
i have strange problem doing reporting: i have numerous clients with different issued invoices.
can anyone help, i have problem doing a sort, I thought i had it
I get this problem while doing some experiment with set . I use a
I have a weird encoding problem from my PyQt app to my mysql database.
Spoiler alert: this is related to Problem 14 from Project Euler. The following code
I have a problem building applicatin with tabBarController. There is no problem doing tabBarController

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.