hey guys,
I'm new to java and was given this assignment in class:
Write an application that determines whether a phrase entered by the user is a
palindrome. A palindrome is a phrase that reads the same backward and forward
without regarding capitalization or punctuation. For example, “Dot saw I was Tod”,
“Was it a car or a cat I saw?”, and “Madam, I’m Adam” are palindromes. Save the file
as Palindrome.java.
so far I have this:
// Purpose: determines whether a phrase is a palindrome // Date: 9/19/2012 // Filename: Palindrome.java // Input: a string from the user // Output: "It is a palindrome" or "It isn't a palindrome" import java.util.Scanner; public class Palindrome { public static void main(String[] args) { //get input Scanner input = new Scanner(System.in); System.out.println("Enter the word or prase: "); String testWord = input.nextLine(); //create variables //upperWord is used to convert the entire phrase to upper so as to ignore the capitalization //reverseWord is the uppercase phrase in reverse StringBuilder upperWord = new StringBuilder(testWord.toUpperCase() + 16); StringBuilder reverseWord = new StringBuilder(upperWord.length() + 16); //reverse the word for(int x = 0; x != (testWord.length() - 1) ; ++x) { reverseWord.setCharAt(x, testWord.charAt(testWord.length() - x - 1)); //ERROR HERE. 0 index is out of bounds. } //compares the two words int counter = 0; char isEqual = 'y'; while(counter != upperWord.length() || isEqual == 'y') { if(upperWord.charAt(counter) != reverseWord.charAt(counter)) isEqual = 'n'; } //display output if(isEqual == 'y') System.out.println(testWord + "is a palindrome!"); else System.out.println(testWord + "is not a palindrome."); } }
I've got a problem with my for loop, which I marked above. I feel like this is a simple fix but haven't been able to figure it out yet. Any help fixing this or making it more efficient would be much appreciated. thanks.


LinkBack URL
About LinkBacks
Reply With Quote