I'm taking a java programing class and I'm having some problems with my second assignment of the semester. Here are the requirements for the assignment:
"A palindrome is a word that is exactly the same when spelt forwards or backwards. Example: "mom". Write a program that reads a word and determines if it is a palindrome. Continue reading and testing words until you encounter the word "quit". Treat uppercase letters as lowercase letters. Write your solution in one class: Palindrome. It should implement a method testWord(String word) that receives a word."
I honestly don't know what I'm doing right now. I've spent hours on this and my brain is just completely fried. I took a look at some examples for code online for Palindromes and that helped a bit.
The errors I'm getting right now are:
- at the "while (newPal != "quit") {" line near the end, they are incomparable types, java.lang.String[] and java.lang.String. How do I manage to compare these two?
- at the "if (isPalindrome == true) {" almost right underneath, it tells me that it "cannot find symbol variable". Anyone know how I can fix this?
My code may be way off, I really don't know at this point. Any help would be GREATLY appreciated. Thanks in advance!
Here's my code so far:
public class Palindrome {
private String pal;
public Palindrome(String initPal) {
pal = initPal.toLowerCase();
// Set the letters of the words to be read in lower case.
}
public boolean isPalindrome() {
int left = 0;
int right = pal.length() -1;
while (left < right) {
if (pal.charAt(left) != pal.charAt(right)) {
boolean isPalindrome = false;
// If the character at the far left is not the same as the one to the far right, the word is not a palindrome.
}
else {
left++;
right--;
// Move to the next letter from the left and the next from the right until the center is reached.
}
}
boolean isPalindrome = true;
// If the center of the word is reached and all the letters matched, the word is a palindrome.
}
public static void main(String[] args) {
String[] newPal = new String[7];
newPal[0] = "redder";
newPal[1] = "Smart";
newPal[2] = "Kayak";
newPal[3] = "Radar";
newPal[4] = "SmoKe";
newPal[5] = "quit";
newPal[6] = "madam";
// Enter the list of words to be checked.
for (String word : newPal) {
while (newPal != "quit") {
// If the word is not "quit", continue the process.
if (isPalindrome == true) {
System.out.println(newPal + "IS a palindrome!");
}
else {
System.out.println(newPal + "is NOT a palindrome!");
}
}
}
}
}

New Topic/Question
Reply




MultiQuote






|