Please enter a String: ant
Please enter a second String: antarctica
The first string is a substring of the second.
Please enter a String: ant
Please enter a second String: tyrant
The first string is NOT a substring of the second.
Keith Rainey
OCPJP6
Stephen Son wrote:
The first output is correct. However the second output is wrong. "ant" is a substring of "tyrant". What is wrong with my code?
Stephen Son wrote:What do you mean by debugging printouts? I've never heard of those.
Stephen Son wrote:
My logic is I am going to compare the first character of string1 to each of the characters of string2. If a match is not found, the method returns false. If a match is found, then I will compare the second character of string1 to each of the characters of string2 and so on... Is my logic correct?
Stephen Son wrote:My logic is I am going to compare the first character of string1 to each of the characters of string2. If a match is not found, the method returns false.
If a match is found, then the second character of string1 will be compared to the character that succeeds the matching character of string2. If those don't match, the method returns false. If they do match, the program will continue and so on.
Paul Clapham wrote:
Stephen Son wrote:My logic is I am going to compare the first character of string1 to each of the characters of string2. If a match is not found, the method returns false.
Your code doesn't do that. In this algorithm you treat the first character of string1 specially -- as you should -- but in your code you don't. It does the same thing for all characters of string1.
Paul Clapham wrote:
If a match is found, then the second character of string1 will be compared to the character that succeeds the matching character of string2. If those don't match, the method returns false. If they do match, the program will continue and so on.
Your code doesn't do that. There isn't anything there which looks for the character after the one you just looked at. It just goes through all characters in string2 every time.
Raymond Tong wrote:May be you could break down your logic to steps.
And put comment next to your codes about the "step" it implemented.
And as suggested above, add debugging printout. (Insert System.out.println without removing existing code)
Stephen Son wrote:I tested both examples and both times the program says "The first string is a substring of the second."
Raymond Tong wrote:May be you could break down your logic to steps.
And put comment next to your codes about the "step" it implemented.
I am going to compare the first character of string1 to each of the characters of string2
Stephen Son wrote:
Even if I break down my logic into steps, I still don't know how to write code for each step. I read your explanation on debugging printout but I'm still not sure what it is. Can you show me an example?
Stephen Son wrote:I read your explanation on debugging printout but I'm still not sure what it is. Can you show me an example?
Please enter a String: ant
Please enter a second String: bath
MATCH: N The value of c and d: a and b
MATCH: Y The value of c and d: a and a
MATCH: N The value of c and d: a and t
MATCH: N The value of c and d: a and h
MATCH: N The value of c and d: n and b
MATCH: N The value of c and d: n and a
MATCH: N The value of c and d: n and t
MATCH: N The value of c and d: n and h
MATCH: N The value of c and d: t and b
MATCH: N The value of c and d: t and a
MATCH: Y The value of c and d: t and t
MATCH: N The value of c and d: t and h
The first string is a substring of the second.
OCPJP 6
OCE J2EE 6 JSP and Servlets Developer
Stephen Son wrote:I made some changes to my code and it seems to be working correctly. All the strings I've tested seem to work. However I'm not 100% certain my code is correct. Is there anything wrong with it?
Raymond Tong wrote:
Stephen Son wrote:I made some changes to my code and it seems to be working correctly. All the strings I've tested seem to work. However I'm not 100% certain my code is correct. Is there anything wrong with it?
If you take "abc" and "bc", you expected to return "NOT" a substring but not the case.
There are 2 tips for you.
1) As soon as you know string1 is impossible to be a substring of string2, you could return false immediately without further checking...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephen Son wrote:@Henry Wong Ok I see. Here are the steps:
1)compare the first character of string1 to each of the characters of string2
2)If a match is not found, the method returns false
If a match is found, proceed to step 3
3)the second character of string1 will be compared to the character that succeeds the matching character of string2
4)If a match is not found, the method returns false
If a match is found, proceed to step 4
5)the third character of string1 will be compared to the character that succeeds the next matching character of string2
6)Repeat
Stephen Son wrote:This is the output:
Please enter a String: ant
Please enter a second String: bath
MATCH: N The value of c and d: a and b
MATCH: Y The value of c and d: a and a
MATCH: N The value of c and d: a and t
MATCH: N The value of c and d: a and h
MATCH: N The value of c and d: n and b
MATCH: N The value of c and d: n and a
MATCH: N The value of c and d: n and t
MATCH: N The value of c and d: n and h
MATCH: N The value of c and d: t and b
MATCH: N The value of c and d: t and a
MATCH: Y The value of c and d: t and t
MATCH: N The value of c and d: t and h
The first string is a substring of the second.
How do I do the next step?
Stephen Son wrote:@Henry Wong I'm not trying to avoid anything. I don't know why you think that.
Stephen Son wrote:I rewrote the instructions and made them more clear.
0)check if string2 is longer than string1, if string2.length() > string1.length() proceed to step 1. If not, return false.
1)compare the first character of string1 to each of the characters of string2
2)If a match is never found, the method returns false
If a match is found eventually, count will record the position of the match. Break and proceed to Part2 of program
3)the second character of string1 will be compared to the character that succeeds the matching character of string2
4)If a match is not found, the method returns false
If a match is found, proceed to step 4, counter will record the position of the new match
and it will be added to count so that the loop will start at this position the next time.
5)the third character of string1 will be compared to the character that succeeds the next matching character of string2
6)Repeat until all characters of string1 have matched(return true) or until a match is not found(return false).
The messages tell me that the first character of string1 is being compared to each character of string2 and so on. Only the count/counter is missing so I added it to my code earlier. Yes I understand what the computer is doing. The loop should break after a match is found so I added that to my code earlier. What is the next step after that?
Henry Wong wrote:Take out a paper and pencil, take your instructions, and try to follow it to the letter. Pretend that you don't know what the purpose of the instructions are, and that someone else wrote it. You will realize that (1) some parts of those notes are vague, and (2) other parts of those notes are just wrong -- getting you wrong answers.
You need to get these instructions clear. And you need to get to the instructions that work. Otherwise, you just have broken instructions, that you are going to try to translate to code.
Stephen Son wrote:@Winston Gukowski What interpretation? If you didn't know, I actually wrote the problem. The problem in the book asks to use indexOf method. My teacher asked us not to...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here