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

  • SEARCH
  • Home
  • 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 8491755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:31:29+00:00 2026-06-10T22:31:29+00:00

so basically, a user inputs 2 strings ( CATSATONTHEMAT AT ) and we need

  • 0

so basically, a user inputs 2 strings ( CATSATONTHEMAT AT ) and we need to count how many time the second string appears in the first string ( so the answer here is 3 )

this is what I have so far, and it keeps saying

“Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 81223
at java.lang.String.substring(Unknown Source)
at practice.main(practice.java:60)”

any help would be appreciated! I just can’t see to find where I went wrong

    String s = scan.next(); // CATSATONTHEMAT
    String t = scan.next(); // AT

    int j= 0;

    for ( int i = 0 ; i < s.length(); i++){
        int k = t.length();
        String newstring = s.substring(i,i+k); // I printed this and the substring works so the if statement might not be working..

        if(newstring.equals(t))
            j++;   // if the new substring equal "AT" then add 1
        }

    System.out.printf("%d", j);  // suppose to print just 3
  • 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-10T22:31:30+00:00Added an answer on June 10, 2026 at 10:31 pm

    The outOfBounds exception happens when i is near the end of s and k takes you passed the end of the string.

    You need to change the loop to only go up to s.length()-t.length()

    for ( int i = 0 ; i < s.length()-t.length(); i++){
    

    I would also recommend bringing the int k = t.length() out of the for loop. You don’t need to assign that every iteration as it should be the same for every iteration.

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

Sidebar

Related Questions

I'm basically trying to create a url friendly query string with user input. The
How can I build an android app which basically get the inputs from user
I basically need to get user input: gets.chomp(input?) And then to convert the given
Basically if the user requests: index.php/foo/bar I need /foo/bar and for index.php I need
My application will basically accept user input via the main(String[] args) method. I have
First of all, Console.ReadKey() is not the answer. I need to be able to
My user input is a string I need to split into two parts, (1)
Basically I want 200 characters maximum to come up in Console.ReadLine() for user input
I have a web site with two routes. Basically the user can do one
I want to make a site where there user can basically navigate the web

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.