Results 1 to 8 of 8
- 07-15-2012, 06:13 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
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); } }
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; }
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.
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; }
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.
- 07-15-2012, 07:56 PM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 12
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; }
For example, suppose that ss1 is a variable of type StarterString whose letters
consist of Programming
- 07-16-2012, 05:09 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Re: Searching for a substring within a String
Thanks for the reply, cselic. Unfortunately that hasn't helped me at all though.
- 07-16-2012, 01:15 PM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 12
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
- 07-16-2012, 01:41 PM #5
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
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...
- 07-16-2012, 07:24 PM #6
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Re: Searching for a substring within a String
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.
- 07-16-2012, 07:44 PM #7
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
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.
- 07-16-2012, 08:12 PM #8
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Re: Searching for a substring within a String
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
-
A string and substring problem
By calnastic in forum New To JavaReplies: 4Last Post: 07-18-2011, 09:02 PM -
How do I remove a substring from a string?
By Unnel in forum New To JavaReplies: 16Last Post: 07-11-2011, 11:56 AM -
Search Substring in String Help Please
By Kestrel01 in forum New To JavaReplies: 3Last Post: 10-26-2010, 06:48 PM -
Reverse a string not using the substring method
By kathyla18 in forum New To JavaReplies: 17Last Post: 04-08-2009, 04:08 AM -
String substring function
By ravian in forum New To JavaReplies: 6Last Post: 01-02-2008, 07:35 PM
Bookmarks