• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

common suffix code throwing null

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should ask yourself another question: what happens if s1 is longer than s2?
 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Master Rancher
Posts: 4280
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic