wood burning stoves 2.0*
The moose likes Applets and the fly likes palindrome checker, tried to debug, keeps looping why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "palindrome checker, tried to debug, keeps looping why?" Watch "palindrome checker, tried to debug, keeps looping why?" New topic
Author

palindrome checker, tried to debug, keeps looping why?

Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28

if anyone could tell me whats going on here, i ran it through the a debugger but i am not yet experianced enough to identify the issue and fix it, any tips would be appreciated. thanks!
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

you never increment the positions of the letters you are comparing, so the program compares the first and last letter forever.


Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
Hunter McMillen wrote:you never increment the positions of the letters you are comparing, so the program compares the first and last letter forever.


Hunter

thanks!! makes sense !! awesome
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28

heres the new code, now its always set to true.. ill debug for a bit but any tips would be appreciated thanks in advance
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

hmm, well you aren't incrementing in the correct place. Try this, ignore the program for a bit and explain to me how you would determine whether a word was a palindrome on a piece of paper.


Hunter
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
Hunter McMillen wrote:hmm, well you aren't incrementing in the correct place. Try this, ignore the program for a bit and explain to me how you would determine whether a word was a palindrome on a piece of paper.


Hunter

you would compare the first and last, the second and second to last the third and third to last...
correct?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

correct, so look at your loop and trace what you are comparing by hand. I'll use the example word "hannah" and do the first letter.



which letter positions do you need to increment/decrement to compare ( a and a)??
which letter positions are you incrementing?

Hunter
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18116
    
    8

Anthony Pena wrote:
Hunter McMillen wrote:hmm, well you aren't incrementing in the correct place. Try this, ignore the program for a bit and explain to me how you would determine whether a word was a palindrome on a piece of paper.


Hunter

you would compare the first and last, the second and second to last the third and third to last...
correct?


No, that isn't correct. You haven't said how or when you stop comparing. And your instructions don't make sense if there are only two letters, for example. You need to be clearer about such things.
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
Paul Clapham wrote:
Anthony Pena wrote:
Hunter McMillen wrote:hmm, well you aren't incrementing in the correct place. Try this, ignore the program for a bit and explain to me how you would determine whether a word was a palindrome on a piece of paper.


Hunter

you would compare the first and last, the second and second to last the third and third to last...
correct?


No, that isn't correct. You haven't said how or when you stop comparing. And your instructions don't make sense if there are only two letters, for example. You need to be clearer about such things.

well you stop comparing when you get to the middle of the word where both letters would be the same. if there are only to letters they would need to be the same?
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
Hunter McMillen wrote:correct, so look at your loop and trace what you are comparing by hand. I'll use the example word "hannah" and do the first letter.



which letter positions do you need to increment/decrement to compare ( a and a)??
which letter positions are you incrementing?

Hunter

i need to increment both the end the first letter? but will this work for smaller words?
and what would happen in the middle if in a word like middle for instance? the rest of the word doesn't match but the two middle letters do.. hmm
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Paul I had asked him to imagine if he were finding this out on paper, there would be no more letters once you got to the middle.

Hunter
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

i need to increment both the end the first letter? but will this work for smaller words?

Try it out and let me know what you find.

Hunter
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
Hunter McMillen wrote:
i need to increment both the end the first letter? but will this work for smaller words?

Try it out and let me know what you find.

Hunter





now, this works for the bigger words but any word with duplicate letters in the middle doesn't work (tried racecar and it returns true tried programming and it returned false but with the word cool it retured true..)
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

There is a different case you will have to handle in your loop depending on whether the word you are looking at has an odd or even number of letters.

Hunter
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
Hunter McMillen wrote:There is a different case you will have to handle in your loop depending on whether the word you are looking at has an odd or even number of letters.

Hunter

so for example, if the first and the last letters dont match, i want to stop it there and say its not a palindrome right away,
if they do match but the next to letters dont i want to stop it there, etc etc.. hm
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Yes that is true, but what I meant is that for some words i.e. 'hannah' you can compare pairs of letters until i_Fstlet index and i_Laslet index pass the i_Midlet index. This method wouldn't work for a word like 'racecar' because there isn't another letter to compare to the 'e' racecar is still a palindrome however, so you have to consider how you would handle that.

Hunter
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Actually I was mistaken, the way you have your condition set up will work for both odd and even numbered palindromes. congrats.

Hunter
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
Hunter McMillen wrote:Actually I was mistaken, the way you have your condition set up will work for both odd and even numbered palindromes. congrats.

Hunter

gah ok, well then how would i make it check against the first two letters and the middle two letters a case like bannah(just and example) my current code, would turn that out to be true
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Think about where you should be incrementing. Should you be doing it before you compare or after?

Hunter
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
Hunter McMillen wrote:Actually I was mistaken, the way you have your condition set up will work for both odd and even numbered palindromes. congrats.

Hunter



woo got it working thanks for all the help really appreciate it
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I'm glad that it works like that, but there is still a better way of solving this that doesn't require those additional variables. Remove those variables and try moving around where you increment i_Fstlet and decrement i_Laslet.

Hunter
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
Hunter McMillen wrote:I'm glad that it works like that, but there is still a better way of solving this that doesn't require those additional variables. Remove those variables and try moving around where you increment i_Fstlet and decrement i_Laslet.

Hunter

after i compare.. that way it compares the first two letters then returns a value before it even increments
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: palindrome checker, tried to debug, keeps looping why?
 
Similar Threads
palindrome program
Palindrome
need some help understading why this is not working (palindrome loop)
jButtons are driving me crazy
need help with applet ActionListener