0

Why do i get a failed test when i run it print ture

Write the method isPalindrome that takes in a String as a parameter and returns true if it is a Palindrome and false if it is not a Palindrome. Spaces and capitalization do not matter. isPalindrome("Bob") ------> True isPalindrome("Taco Cat") ------> True isPalindrome("small Dragon") ------> False isPalindrome("Never Odd or even") ------> True isPalindrome("Turkey") ------> False class Main { public static void main(String[] args) { System.out.println(isPalindrome("Bob")); // you can add more test cases here } public static boolean isPalindrome(String word) { int left = 0, right = word.length() - 1; while(left < right) { if(word.charAt(left) != word.charAt(right)) { return true; } left++; right--; } return false; //write code here } } What can I improve to make sure it passes the test.

11th Dec 2022, 6:14 PM
JOHN DELFIN
JOHN DELFIN - avatar
4 Réponses
+ 2
What is the test? Can you add what you need to do? Also, the best way to have people view your code is to copy, paste and save it in the code playground. Then, you can attach it here.
11th Dec 2022, 7:25 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
JOHN DELFIN are the spaces already removed from the string before it gets passed to isPalindrome? You can use the string method .replaceAll() to remove them.
11th Dec 2022, 8:59 PM
Brian
Brian - avatar
0
Updated
11th Dec 2022, 8:10 PM
JOHN DELFIN
JOHN DELFIN - avatar
0
Remove spaces (replaceAll(" ", "")), make String case insensitive (for example: toLowercase()). Boolean return values are swapped (invert trues/falses).
12th Dec 2022, 12:42 AM
comtur
comtur - avatar