lundi 18 juillet 2016

If Condition Within Java Method Keeps Giving False [duplicate]

This question already has an answer here:

Below Java code needs to reverse the input of the user and check if word entered and reverse are identical. Even when I enter aba, which is identical in reverse too, I get back false printed. Anyone knows why?

import java.util.Scanner;

public class Palindromes
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      
      System.out.println("Please enter a word:");
      String userWord = input.nextLine();
        
      System.out.println(isPalindrome(userWord));
   }
   /* Reverse method returns parameter in reverse */
   public static String reverse(String word)
   {
      String reversed = "";
      
      for(int i=(word.length()-1); i>=0; i--){
         reversed += word.charAt(i);
      }
      
      return reversed;
   }
   /* Checks the condition if the word is Palindrome or not */
   public static boolean isPalindrome(String word) 
   {
      boolean status = true;
      
      if( word == (reverse(word)) ){
         status = true;
      } else {
         status = false;
      }
      
      return status;      
   } 
}

Aucun commentaire:

Enregistrer un commentaire