help with string method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PvtBillPilgrim
    New Member
    • May 2007
    • 19

    #1

    help with string method

    Hi. I just started Java and need some help with a method involving strings.

    I need to return the index of the start of the first occurence of one string (parameter two) in another string (parameter one).

    I tried this:
    [code=java]
    public static int findInString (String text1, String text2)
    {
    int length1 = text1.length();
    int length2 = text2.length();
    int index;
    char x = text2.charAt(0) ;
    for (index = 0; index < length1; index++)
    {
    char y = text1.charAt(in dex);
    if (x == y)
    {
    text 1 = text1.substring (index, index + length2);
    if (text1 == text2)
    {
    return index;
    }
    }
    if (index == length1)
    {
    return -1;
    }
    }
    [/code]

    NOTE: I cannot use the indexOf method. It's for a programming class at a local university and he won't let us use it. So I need to find an alternative way to basically do what it does.

    My code compiles; it's just wrong semantically. Any help would be appreciated.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Try using .equals instead of ==

    Comment

    • PvtBillPilgrim
      New Member
      • May 2007
      • 19

      #3
      Can you just tell me if this makes any sense?

      I think it's fairly intelligent in just thinking about it. But I'm not sure if it's doing exactly what I want it to do.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by PvtBillPilgrim
        Can you just tell me if this makes any sense?

        I think it's fairly intelligent in just thinking about it. But I'm not sure if it's doing exactly what I want it to do.
        Use the .equals method for camparing Strings as has been suggested then test your code and you'll see whether it's correct or not.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by PvtBillPilgrim
          NOTE: I cannot use the indexOf method. It's for a programming class at a local university and he won't let us use it. So I need to find an alternative way to basically do what it does.

          My code compiles; it's just wrong semantically. Any help would be appreciated.
          If you're planning to use the substring() method why not let that method do all
          the work? Suppose you have a String S of length n and a pattern P of length m.

          If m > n you're sure you can't find the pattern in String S. otherwise, for all
          positions i in [0, n-m] take the substring of length m starting at position i and
          compare that substring with your pattern P.

          In other words, there's no need to search for that first character in S and P to
          compare that for equality.

          kind regards,

          Jos

          Comment

          • PvtBillPilgrim
            New Member
            • May 2007
            • 19

            #6
            OK. I'm trying this with no luck (but it looks fine to me).

            I have the easy cases in my program; I'm just not going to include them here:
            [code=java]
            String mutation;
            int index = 0;
            while (index < (lengthStr - lengthSubstring ))
            {
            mutation = str.substring(i ndex, index + lengthSubstring );
            if (mutation.equal s(substring))
            {
            position = index;
            break;
            }
            index++;
            }
            position = -1;
            }
            return position;
            }
            [/code]
            I get -1 everytime except for when the strings are equal (since the cases not included here work fine).

            What am I doing wrong? It compiles, just doesn't work.
            Last edited by JosAH; Jun 2 '07, 03:39 PM. Reason: added[code=java] ... [/code] tags

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by PvtBillPilgrim
              OK. I get -1 everytime except for when the strings are equal (since the cases not included here work fine).

              What am I doing wrong? It compiles, just doesn't work.
              Even if you set your 'position' variable to the correct value in your while loop
              you destroy it again just after the loop again, i.e. you explicitly set it to -1.

              Do something like this instead;[code=java]
              int subPosition(Str ing s, String p) {

              int sn= s.length();
              int pn= p.length();

              for (int i= 0; i <= sn-pn; i++) {
              if (s.substring(i, i+pn).equals(p) )
              return i;
              }
              return -1;
              }[/code]

              Comment

              • PvtBillPilgrim
                New Member
                • May 2007
                • 19

                #8
                OK. Sorry about this.
                Everything works now, except for when the substring does not exist within the string and the string has more characters than the substring (i.e. the following loop).

                .............. (I again omitted the if and else if at the beginning because I'm sure they work)
                else
                {
                for (int i = 0; i < lengthStr; i++)
                {
                String mutation;
                mutation = str.substring (i, i + lengthSubstring );
                if (mutation.equal s(substring))
                {
                position = i;
                return position;
                }
                }
                position = -1;
                return position;
                }
                }

                Again it compiles. But if I try something like "hamburger" for the larger string and "dog" for the substring, it gives me like ten red lines telling me my string index is out of range (10 with this instance).

                Is there an easy way to solve this without jeopardizing the code that does work?
                Thanks again (I'm just frustrated at this point.)

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by PvtBillPilgrim
                  OK. Sorry about this.<snip>
                  Again it compiles. But if I try something like "hamburger" for the larger string and "dog" for the substring, it gives me like ten red lines telling me my string index is out of range (10 with this instance).

                  Is there an easy way to solve this without jeopardizing the code that does work?
                  Thanks again (I'm just frustrated at this point.)
                  No need to apologize; you're working on your problem and there's nothing wrong
                  with that. Think of it, suppose you have a String of five characters "abcde" and
                  suppose you have a pattern of two characers: "de". What would the maximum
                  value of i be from where you start your substring? Remember positions start at
                  zero. In order to rip out a substring of two characters you can rip out your
                  substring starting at position 5-2 == 3 at most. Further than that position you
                  can't build a substring of two characters.

                  This basically translates to: your for loop shouldn't loop over all possible values
                  less than the String's length. It should take the length of the pattern in account
                  too; reread my code snippet in my previous reply.

                  kind regards,

                  Jos

                  Comment

                  Working...