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 6024597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:10:44+00:00 2026-05-23T04:10:44+00:00

I need to write method that will chek String str on other string, and

  • 0

I need to write method that will chek “String str” on other string, and return the index that the str starts.

That’s sound like homework, and it is some of homework but for my use to learn for a test…

i’ve tried:

public int IndexOf (String str) {

   for (i= 0;i<_st.length();i++)
   {
        if (_st.charAt(i) == str.charAt(i)) {
            i++;
            if (_st.charAt(i) == str.charAt(i)) {
                return i;
            }
        }
   }
   return -1;
 }

but i dont get the right return. why? i’m on the right way or don’t even close?

  • 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-05-23T04:10:45+00:00Added an answer on May 23, 2026 at 4:10 am

    I am afraid, you are not close.

    Here’s what you have to do:

    • Loop on the characters of the string (the one on which you are supposed to do an indexOf, I will call this the master) (you are going this right)
    • For every character check whether your other string’s character and this character are the same.
    • If they are (a potential start of the same sequence) check whether the next characters in the master match with your String to check (You might want to loop through the elements of the string and check one by one).
    • If they don’t match, continue with the characters in the master string

    Something like:

    Loop master string
    for every character (using index i, lets say)
       check whether this is same as first character of the other string
       if it is
          //potential match
          loop through the characters in the child string (lets say using index j)
    
          match them with the consecutive characters in the master string 
          (something like master[j+i] == sub[j])
    
           If everything match, 'i' is what you want
           otherwise, continue with the master, hoping you find a match
    

    Some other points:

    • In java, method names start with a
      lower case letter by convention
      (meaning, the compiler won’t
      complain, but your fellow programmers
      may). So IndexOf should actually be
      indexOf
    • Having instance variables
      (class level variables) start with a
      _ (as in _st) is not a really good
      practice. If your professor insists,
      you may not have many options, but
      keep this in mind)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a unit test for a method that will print a
I'm trying to write a method that will operate like this: NSString *responseData =
I need to write a method that loops through ArrayList<String> pathClientStatic and then copies
I'm trying to write a method that will return a Hibernate object based on
I need to write an extension method on a byte[]. Is that possible?
I need to write a procedure that will allow me to select x amount
I'm trying to write a method that will get a private field in a
Hey, I need to write a program for OSX that will cause my macbook
I need to write a base class (in this example, class A) that will
I am trying to write a method that loops and repeatedly calls other methods.

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.