This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes common suffix code throwing null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "common suffix code throwing null" Watch "common suffix code throwing null" New topic
Author

common suffix code throwing null

Albert Park
Greenhorn

Joined: Feb 01, 2012
Posts: 28
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

Joined: Sep 18, 2000
Posts: 557
    
    7

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

Joined: Feb 01, 2012
Posts: 28
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

Joined: Sep 18, 2000
Posts: 557
    
    7

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

Joined: Feb 01, 2012
Posts: 28
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

You should ask yourself another question: what happens if s1 is longer than s2?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

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.


Ashwin Sridhar
SCJP | SCWCD | OCA
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7489
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

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

Joined: Mar 05, 2008
Posts: 2982
    
    9
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

Joined: Jul 09, 2011
Posts: 272

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: common suffix code throwing null
 
Similar Threads
Comparator Doubt
Compare() - from Master Exam
Encryption
Palindrome
Recursive anagram problem