Results 1 to 8 of 8
  1. #1
    Tryx is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Searching for a substring within a String

    Hi all. Really new to all of this Java stuff so I hope you can bear with me.
    For starters, here's my constructor:
    Java Code:
       public StarterString(String aString)
       {
          letters = new char[aString.length()];      
          for (int count = 0; count < aString.length(); count++)
          {
             letters[count] = aString.charAt(count);         
          }
       }
    And here's a method I've had to code for the first part of my question.
    Java Code:
    /**
        * Checks that the argument String and receiver String
        * are equal, first in length and then that each character
        * in each index is correct. Returns true if correct, false
        * if not
        */
       public boolean equals(StarterString aStarterString)
       {
          boolean equal = true;
          int receiverLength = this.length();
          int argumentLength = aStarterString.length();
          int i = 0;
          if (receiverLength == argumentLength)
          {
             while ((i < argumentLength) && (equal))
             {
                if (this.letters[i] != aStarterString.letters[i])
                {
                   equal = false;
                }
                i++;
             }
          }
          else
          {
             equal = false;
          }
          return equal;      
       }
    Now this is where I get stumped. Question:

    Write a public instance method for the StarterString class called contains().
    This method takes one argument of type StarterString and returns a boolean
    value.
    This method is similar to the equals() method you wrote in part (ii)(b) except that
    this method returns true if the argument is a substring of the receiver, false
    otherwise.
    For example, suppose that ss1 is a variable of type StarterString whose letters
    consist of Programming and that ss2 is a variable of type StarterString whose
    letters consist of ing, then the message expression ss1.contains(ss2) would
    return true.
    To write this method you will need an outer loop that repeats the length of the
    receiver the length of the argument + 1 times or until a match is found. Each
    time through the loop the method should compare the argument with part of the
    receiver that is the same length as the argument. For example, to find whether ing
    is a substring of Programming the method should first compare ing with Pro,
    then with rog, then with ogr, until finally ing is matched with ing on the ninth
    attempt. The loop ends and true is returned.
    I am utterly stumped at the moment. I have literally zero idea of how to efficiently use a substring to search through another String in such a way as the course materials haven't provided with any real useful info on the subject. It's mentioned in the question that it's similar to the equals() method that I wrote above, but that's not been much help. I've slaved over this all weekend and come up with a very crass solution in this:

    Java Code:
    public boolean contains(StarterString aStarterString)
       {
          boolean match = false;
          int receiverLength = this.length();
          int argumentLength = aStarterString.length();
          int length = receiverLength - argumentLength + 1;
          int j = 0;
          int k = j + 1;
          int l = j + 2;
          System.out.print("Length is " + length + "\n\n");
          while ((j < length) || (match))
          {         
             if (this.theLetters[j] == aStarterString.letters[0])
             {
                if (this.theLetters[k] == aStarterString.letters[1])
                {
                   if (this.theLetters[l] == aStarterString.letters[2])
                   {
                      match = true;
                      return match;
                   }               
                }            
             }
             j++;
             k++;
             l++;
          }
          match = false;
          return match;
       }
    But while it kind of seems to do roughly what is needed, I'm completely positive that this isn't the correct solution to the question that the assignment is asking, especially as it only works when the substring has three characters, and later test examples use substrings with four and five characters, which I hadn't realised while coming up with this solution.

    So what would be the correct method of using a substring to iterate through a longer string until a match is found?

    Many thanks for your time reading this.

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Searching for a substring within a String

    Let's say that you have just this at the beginning:
    Java Code:
    public boolean contains(StarterString ss2) {
    	return false;
    }
    How you're going to write code for this:
    For example, suppose that ss1 is a variable of type StarterString whose letters
    consist of Programming

  3. #3
    Tryx is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: Searching for a substring within a String

    Thanks for the reply, cselic. Unfortunately that hasn't helped me at all though.

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Searching for a substring within a String

    You should go step by step to learn, and improve you knowledge.
    First you should learn how to use StarterString class and StarterString constructor. I asked you that question because understanding and solving that question is first step for writing code for contains method. I saw that you've already written that method but you don't have any variable of type StarterString and you must have one. Point of writing StarterString constuctor is to use it everytime when you need it, and not just write it to have it but never use it.

    For example, suppose that ss1 is a variable of type StarterString whose letters
    consist of Programming
    If you doesn't understand something about this you may ask a question.

  5. #5
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Searching for a substring within a String

    I was going to suggest the section on "Finite Automata", but it's still way more complex than you need or could use.

    You're on the right track with your code, but here's a question - what happens if the word is 4 characters long? You only looked for 3 chars in a row. You know the length of each Char array, so it shouldn't be too hard to iterate over the string from a start position for a given length. Just remember what happens when you try to access an index that's out of bounds...

  6. #6
    Tryx is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: Searching for a substring within a String

    Quote Originally Posted by Diargg View Post
    I was going to suggest the section on "Finite Automata", but it's still way more complex than you need or could use.

    You're on the right track with your code, but here's a question - what happens if the word is 4 characters long? You only looked for 3 chars in a row. You know the length of each Char array, so it shouldn't be too hard to iterate over the string from a start position for a given length. Just remember what happens when you try to access an index that's out of bounds...
    Yeah, the possibility of 4 or more character substrings is where I got stuck. That wasn't mentioned in the question until right at the very end, so I went to work believing I only had to search for 3 character substring.

    I'm just seriously struggling to find that eureka moment with this. I understand how to iterate over the string, searching for a single character. That's fine and well. But I just can't get my head around iterating over a string, searching for a match of a smaller string.

  7. #7
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Searching for a substring within a String

    I think you might be taking it on in chunks that are too large.

    So you've already successfully iterated over an array of chars in your code. Perhaps, for each position, you could then start at the beginning of the other string, and walk along them both, side by side.

    Put another way - how would you check if it were 4 characters? 5? You'll need another loop, I can tell you that.

  8. #8
    Tryx is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: Searching for a substring within a String

    Quote Originally Posted by Diargg View Post
    I think you might be taking it on in chunks that are too large.

    So you've already successfully iterated over an array of chars in your code. Perhaps, for each position, you could then start at the beginning of the other string, and walk along them both, side by side.

    Put another way - how would you check if it were 4 characters? 5? You'll need another loop, I can tell you that.
    That's actually the most helpful bit of advice I've been given so far, thanks.

    The second loop, I hadn't even considered that, to be honest.

    I can visualise in my head what I need to do, the difficulty is just going to lie in whether the I can translate the process in my head into code that works on the screen. :|

    Brain is fried.

Similar Threads

  1. A string and substring problem
    By calnastic in forum New To Java
    Replies: 4
    Last Post: 07-18-2011, 09:02 PM
  2. How do I remove a substring from a string?
    By Unnel in forum New To Java
    Replies: 16
    Last Post: 07-11-2011, 11:56 AM
  3. Search Substring in String Help Please
    By Kestrel01 in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 06:48 PM
  4. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 04:08 AM
  5. String substring function
    By ravian in forum New To Java
    Replies: 6
    Last Post: 01-02-2008, 07:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •