Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

palindrome checker, tried to debug, keeps looping why?

 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you never increment the positions of the letters you are comparing, so the program compares the first and last letter forever.


Hunter
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about where you should be incrementing. Should you be doing it before you compare or after?

Hunter
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic