I'm trying to write a code in java that will read in any palindrome word or phrase a user enters and determine whether it is a palindrome or not.
I must read in a series of phrases. For each, if it is a plaindrome print "yes", otherwise print "no". Stop when the user types in "done". I also must use a variable of type 'boolean' and a compound boolean expression.
In addition, I have to ignore capitalization and spaces (I think I've included this), and assume the only punctuation marks in the input are: comma (,), period (.) and apostorphe ('), and assume that the user types at least one letter (i.e. ignoring the empty string as input).
Here is my code so far...not sure what to include from here, as the console automatically terminates:
import java.util.Scanner;
import javax.sound.sampled.DataLine.Info;
/*
* Implementation Plan
* 0. Copy the skeleton
* 1. Print message
* 2. Read entered palindrome
* 3. Write loop
* 4. Recognize palindrome word
* 5. Recognize palindrome phrase
* 6. Recognize non-palindrome
* 7. Answer yes for palindrome
* 8. Answer no for non-palindrome
* 9. Run error testing
* Test Plan
*
*/
public class Palindrome {
public static void main(String[] args) {
String phrase;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a possible palindrome(Enter done to stop)");
// Java will read the entire line into the variable 'phrase'.
phrase = keyboard.nextLine();
int index;
}
public static boolean isPalindrome(String s) {
s = s.toLowerCase();
s = s.trim();
if (isPalindrome(s))
System.out.println("Yes");
else
System.out.println("No");
if (s.length() <= 1)
return true; // Base case
else {
if (s.charAt(0) == s.charAt(s.length() - 1))
return isPalindrome(s.substring(1, s.length() - 1));
else
return false;
}
}
}
Thanks, all!

New Topic/Question
Reply




MultiQuote








|