Intro to java assignment help
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
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"
public class Palindrome
public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.println("Enter the word or prase: ");
String testWord = input.nextLine();
//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';
if(isEqual == 'y')
System.out.println(testWord + "is a palindrome!");
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.
Re: Intro to java assignment help
Please Edit your post and wrap your code with
<YOUR CODE HERE>
to get highlighting and preserve formatting.
What is the value of the index and what is the size of the String being indexed? Remember indexing starts at 0 and goes to the length-1.
index is out of bounds.
Print out the index and the String so you can see what the values the computer sees and determine the problem.