Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

common suffix code throwing null

 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I am having trouble figuring this problem out. I am trying to find the common suffix for two Strings and print it out. For example, two strings values such as "caption","action", I want to have their common suffix return "tion". For some reason, even though there are common suffix value, the result keeps throwing null.
Can you someone please tell me what is wrong with my code?
Also let me know if there is an easier and simpler way to get the same job done.

Thanks in advance!
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

well let's see, where is the problem? is it in commonSuffix or reverse?

Hint - never write more than a few lines of code (1 or 2) with out testing.

So put some print statements to find out where the issue lies. should be simple enough and the problem should become obvious rather easily.

like are you really comparing the last char in s1 with the last char in s2?

And yes i can think of a few ways that are simpler, like......

(and this is a bad, but simpler option)

in a loop - compare two substrings that are the last char of each, if they are equal
compare two substrings that are of size 2, then three ......


-steve

and of course look at the api to see if there are classes / methods to help.
 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice.
The reason why i created reverse method was to print the letters in the right order. My commonSuffix method compares their suffix from right to left, so if i just print that out, it will print backwards.
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why i created reverse method was to print the letters in the right order. My commonSuffix method compares their suffix from right to left, so if i just print that out, it will print backwards.


That is obvious, but why??? Why do what is provided by the language?
 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Fahlbusch wrote:
The reason why i created reverse method was to print the letters in the right order. My commonSuffix method compares their suffix from right to left, so if i just print that out, it will print backwards.


That is obvious, but why??? Why do what is provided by the language?


ahhh ic. I am learning from the text, "Introduction to java programming" and the text wants me to come up with that on my own. I haven't learn the reverse method stored in java yet.
so yea, I just learned something new! haha
sorry for my ignorance...
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should ask yourself another question: what happens if s1 is longer than s2?
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one more question , what if you have following input,

S1 = "abcdef";
S2 = "agchei";

Your code would return matching values at positions 0,2,4 and you would have abc as output but its not the suffix.
 
Winston Gutkowski
Bartender
Pie
Posts: 10106
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Albert Park wrote:so yea, I just learned something new!

Here's something else then: the check 'i > -1' may be slower than 'i >= 0', because the compiler (or CPU) may well be able to optimize the latter. I understand why you did it, but very often the simplest (and, in this case, the most readable) solution is the best.

Another one to think about: What about having your commonSuffix() method simply return the length of the common suffix? What you have certainly isn't wrong, but you might find that approach a bit more flexible.

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Sridhar wrote:And one more question , what if you have following input,

S1 = "abcdef";
S2 = "agchei";

Your code would return matching values at positions 0,2,4 and you would have abc as output but its not the suffix.

It won't.. See the else part.. It breaks as the first mismatch is found from the last..
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Here's something else then: the check 'i > -1' may be slower than 'i >= 0', because the compiler (or CPU) may well be able to optimize the latter.

To be fair, the compiler may well be able to optimize either one, given that for ints they are equivalent. It's difficult to anticipate exactly what optimizations end up getting put into code nowadays, and probably best not to say too much about it unless we're actively timing the results for a specific bit of code.

Winston Gutkowski wrote:I understand why you did it, but very often the simplest (and, in this case, the most readable) solution is the best.

Agree 100%.
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't.. See the else part.. It breaks as the first mismatch is found from the last..


Oh yes. I didn't notice that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic