java - String comparison isn't working properly? -


this question has answer here:

so i'm trying have user input string, print backwards , compare new strings see if palindrome or not... doesn't seem working , i'm not sure why...

public static void main(string[] args) {     scanner input = new scanner (system.in);     system.out.print("enter word: ");     string word = input.next();     stringbuilder drow = new stringbuilder(word);     drow.reverse();     system.out.println(drow);     system.out.print(" ");     string x = drow.tostring();     if (word == x) {         system.out.println("that word palindrome");  } else {     system.out.println("that word not palindrome"); } 

thanks why isn't working...

word == x asks if literally same string (i.e. 2 references pointing same object in memory), not if identically equal (i.e. 2 different strings happen contain same letters), want

string.equals(otherstring) 

the analogy use identical twins. there 2 identical twins. == asks if same person. .equals() asks if same


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -