# Could someone help me with this code?

• January 7th, 2012, 02:00 PM
espltdf100
Could someone help me with this code?
Though my code compiles, it returns all 0's, and I don't understand why.

The problem I have to answer is :

Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.

Example:

stringMatch("xxcaazz", "xxbaaz") → 3
stringMatch("abc", "abc") → 2
stringMatch("abc", "axc") → 0

My code for the method is:

public int stringMatch(String a, String b)
{
int match = 0;
int x = 0;
int y = 2;
while(y < a.length() && y < b.length())
{
if (a.substring(x,y) == b.substring(x,y))
{
match++;
x++;
y++;
}
else
{
x++;
y++;
}
}
return match;
}
• January 7th, 2012, 02:19 PM
Norm
Re: Could someone help me with this code?
Use the equals() method when comparing the contents of two Strings, not the == operator
• January 7th, 2012, 02:21 PM
snowguy13
Re: Could someone help me with this code?

Then, I have a question: do the matching substrings have to start at the same index?
So, for example would

stringMatch("at", "cat");

return 1 or 0?
• January 7th, 2012, 02:31 PM
espltdf100
Re: Could someone help me with this code?

The new code after using the equals method is:

Code Java:

``` public int stringMatch(String a, String b) { int match = 0; int x = 0; int y = 2; String aa = a.substring(x,y); String bb = b.substring(x,y); while(y < a.length() && y < b.length()) { if (aa.equals(bb) ) { match++; x++; y++; } else { x++; y++; } } return match; }```

The substrings are supposed to start at the same index.
Thanks for the responses btw!
• January 7th, 2012, 02:32 PM
Norm
Re: Could someone help me with this code?
Does it work now?
• January 7th, 2012, 02:34 PM
snowguy13
Re: Could someone help me with this code?
Quote:

String aa = a.substring(x,y);
String bb = b.substring(x,y);
You define these at the start of the stringMatch() method, but do you ever change their values? The statement...

if(aa.equals(bb))

...will compare the same thing for each iteration of the while loop unless you change the values of aa and bb.

Quote:

Thanks for the responses btw!
No problem! Happy to help! :D
• January 7th, 2012, 02:37 PM
espltdf100
Re: Could someone help me with this code?
It no longer returns all 0s, but I still don't recieve the expected answer.
The tests are:

stringMatch("xxcaazz", "xxbaaz") → expected 3, got 4
stringMatch("abc", "abc") → expected 2, got 1
stringMatch("abc", "axc") → expected 0, got 0
stringMatch("hello", "he") → expected 1, got 0
stringMatch("he", "hello") → expected1, got 0
stringMatch("aabbccdd", "abbbxxd") → expected 1, got 0
stringMatch("aaxxaaxx", "iaxxai") → expected 3, got 0
stringMatch("iaxxai", "aaxxaaxx") → expected 3, got 0
• January 7th, 2012, 02:39 PM
Norm
Re: Could someone help me with this code?
See post #6
• January 7th, 2012, 02:44 PM
espltdf100
Re: Could someone help me with this code?
The new code is:

Code Java:

```public int stringMatch(String a, String b) { int match = 0; int x = 0; int y = 2; String aa = a.substring(x,y); String bb = b.substring(x,y); while(y < a.length() && y < b.length()) { if (aa.equals(bb) ) { match++; x++; y++; aa = a.substring(x,y); bb = b.substring(x,y); } else { x++; y++; aa = a.substring(x,y); bb = b.substring(x,y); } } return match; }```

I get more correct answers now, though a few still come out incorrect

stringMatch("xxcaazz", "xxbaaz") → expected 3, got 2
stringMatch("abc", "abc") → expected 2, got 1
stringMatch("abc", "axc") → expected 0, got 0
stringMatch("hello", "he") → expected 1, got 0
stringMatch("he", "hello") → expected1, got 0
stringMatch("aabbccdd", "abbbxxd") → expected 1, got 1
stringMatch("aaxxaaxx", "iaxxai") → expected 3, got 3
stringMatch("iaxxai", "aaxxaaxx") → expected 3, got 3
• January 7th, 2012, 02:45 PM
snowguy13
Re: Could someone help me with this code?
Okay, I think I know what the issue is.

while(y < a.length() && y < b.length())

This is tough to explain without giving it away, but here it goes: think about how the substring() method works. If you did a substring from 0 to 1, how many characters of the original String would you get? Now, think about this: if you asked for <String>.substring(0, <String>.length()), how many characters of the String would you get?

I'm sorry for that inadequate guidance, but I can't just hand you the answer. :P
• January 7th, 2012, 02:48 PM
Norm
Re: Could someone help me with this code?
It helps to print out the values of the Strings that are being compared so you can see what the code is doing.
Add some printlns just before the if test with the equals() to show their values. Something like this:
System.out.println("aa=" + aa +"<");
• January 7th, 2012, 03:03 PM
snowguy13
Re: Could someone help me with this code?

Code Java:

```String example = "thunderstorm"; System.out.print(example.substring(0, 3));```
thu
or, to think about it this way...
thunderstorm

So what if I did this:
Code Java:

```String example = "thunderstorm"; System.out.print(example.substring(7, example.length()));```
The output would be:
storm
or, to think about it this way...
thunderstorm

Using the length of a String as an argument in the substring() method works, but your while statement assumes that it doesn't by saying that the ending index (y) must be less than the String's length.

....hopefully that makes more sense...?
• January 7th, 2012, 03:21 PM
espltdf100
Re: Could someone help me with this code?
Code Java:

```public int stringMatch(String a, String b) { int match = 0; int x = 0; int y = 2; String aa = a.substring(x,y); String bb = b.substring(x,y); while(y <= a.length() && y <= b.length()) { aa = a.substring(x,y); bb = b.substring(x,y); if (aa.equals(bb) ) { match++; x++; y++;   } else { x++; y++;   } } return match; }```

lol took me a while but I finally got it. My code works everytime unless one of the parameters has less than 2 characters, but I think I know where to go from here. Thanks for the help snowguy and norm, I really appreciate it :o
• January 7th, 2012, 10:46 PM
snowguy13
Re: Could someone help me with this code?