+ 2

Java Palindrome Problem

It's an assignment I'm working on, I have to modify the code to eliminate the runtime error! It's prints true only if the palindrome has an odd length, false if it's no palindrome but error if it's a palindrome but has an even length. I tried changing the "i != j" in the while loop to "i <= j" and it stops the error but prints false for even length palindrome. Original code below šŸ‘‡ https://code.sololearn.com/cLQ1Dlzp9WMn/?ref=app

8th Jun 2022, 6:53 PM
Rawley
Rawley - avatar
7 Answers
+ 2
Use condition i <= j And return i > j; let's see it works.
8th Jun 2022, 7:07 PM
Jayakrishna šŸ‡®šŸ‡³
+ 3
Yes. That's why I gave hints so you can rethink.. You're welcome..
8th Jun 2022, 7:12 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
It not enough details to find mistakes.. Can you show your code by saving in playground..? edited:
8th Jun 2022, 6:55 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
JayakrishnašŸ‡®šŸ‡³ I already linked the code!
8th Jun 2022, 6:58 PM
Rawley
Rawley - avatar
+ 1
For odd length ex: aba i != j becomes false and returns i==j true But for even length, Ex: abba, at end i=2 , j=1 when loop exits so return i==j returns false. you need to think about logic again.
8th Jun 2022, 7:05 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
I know about that, I don't know what to do that's why I came here
8th Jun 2022, 7:06 PM
Rawley
Rawley - avatar
+ 1
It's working Thanks! Soo close šŸ˜…
8th Jun 2022, 7:09 PM
Rawley
Rawley - avatar