-3

import java.util.Scanner; public class CheckPalindrome3 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your number :");
    String num = input.nextLine();
    String arr = "";
    for(int i = num.length() - 1 ; i >= 0;i--)
    {
    
        arr = arr + num.charAt(i);
    }
System.out.println(arr+"\n"+num);
    if(arr == num) {
        System.out.println("Yes that's a Palindrome !!");
    }
    else
    {
        System.out.println("No that's not a Palindrome !!");
    }

}

}

When I put 121 it printed out No thats not a palindrome but arr is the same as num

1 Answers1

-1

The problem is the test arr == num. For strings, == tests whether the two strings have the same address in memory (which often is not true, even if the strings look the same). Use arr.equals(num) instead. That will test if all of the characters in arr are the same as those in num.

VentricleV
  • 40
  • 6