1

I'm writing a program that finds whether a word/phrase up to 8 characters is a palindrome. No matter what I put in as input, even if it is a palindrome, my program prints my else statement. I checked and made sure the code I wrote actually prints the input in reverse and it does. So I'm not quite sure what the problem is.

import java.util.Scanner;
public class hw5 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String word, newWord;
int lengthOfWord;
char lc;//last character

    System.out.print("Enter word/phrase of 8 characters or less: ");
    word = in.nextLine();

    word = word.toLowerCase();
    newWord = word;
    lengthOfWord = word.length();
    lc = word.charAt(lengthOfWord -1);


    lc = word.charAt(lengthOfWord -1);

    if (word.length() == 2)
        newWord = lc+word.substring(0,1);
    else if (word.length() == 3)
        newWord = lc+word.substring(1,2)+word.substring(0,1);
    else if (word.length() == 4)
        newWord = lc+word.substring(2,3)+word.substring(1,2)+word.substring(0,1);
    else if (word.length() == 5)
        newWord = lc+word.substring(3,4)+word.substring(2,3)+word.substring(1,2)+word.substring(0,1);
    else if (word.length() == 6)
        newWord = lc+word.substring(4,5)+word.substring(3,4)+word.substring(2,3)+word.substring(1,2)+word.substring(0,1);
    else if (word.length() == 7)
        newWord = lc+word.substring(5,6)+word.substring(4,5)+word.substring(3,4)+word.substring(2,3)+word.substring(1,2)+word.substring(0,1);
    else if (word.length() == 8)
        newWord = lc+word.substring(6,7)+word.substring(5,6)+word.substring(4,5)+word.substring(3,4)+word.substring(2,3)+word.substring(1,2)+word.substring(0,1);
    else
        newWord = "error, not enough or too many characters";

    if (newWord == word)
        System.out.println("it is a palindrome");
    else
        System.out.println("it is not a palindrome");

    System.out.println(newWord);
Cooper Taylor
  • 51
  • 1
  • 2
  • 6
  • 1
    Use `String`'s `equals` method to compare string values, not the `==` operator, which determines if two references refer to the same object. – rgettman Sep 25 '13 at 23:41

4 Answers4

4

You could make it a lot simpler.

word = word.toLowerCase();
newWord = new StringBuilder(word).reverse().toString();

if (newWord.equals(word))
     System.out.println("it is a palindrome");
 else
      System.out.println("it is not a palindrome");
Derek
  • 7,615
  • 5
  • 33
  • 58
3

== means the runtime will check if they are pointing to the same address, equals will check if the two objects have the same content. So try to use the equals method to compare Strings and == for numbers.

n00begon
  • 3,503
  • 3
  • 29
  • 42
Spark8006
  • 635
  • 1
  • 7
  • 15
3

Strings are objects. And the string variables are merely pointers to the string object. So, when you do if(newWord==word), you're not comparing the contents of the strings, rather you're comparing the value in these pointers (the memory location the pointers point too). You need to use String's equals method in order to compare the contents of two strings.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
2
if (newWord.equals(word))
        System.out.println("it is a palindrome");
else
        System.out.println("it is not a palindrome");

When you use ==, the words are not compared, what is compared is the index of the two variables in memory.

Arash Saidi
  • 2,228
  • 20
  • 36