dimanche 18 mars 2018

Why is this if statement in my code ignored?

This code is supposed to reverse a String and then check with if statement if the original String input equals the reversed input. But the if statement never runs, indicating that original String and reversed one aren't the same... even when I input a palindrome.

import java.util.Scanner;

public class Test_ScannerPalindrome {
    static String reverse(String par) {
        String Reversed = new String();
        for(int i = par.length()-1; i >= 0; i--) {
            Reversed += par.charAt(i);
        }
        return Reversed;
    }
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        String Unos = sc.nextLine();
        if(Unos == reverse(Unos)) {
            System.out.println(Unos + " is palindrome");
        } else{
            System.out.println(Unos + " is not palindrome");
        }

    }

}

Aucun commentaire:

Enregistrer un commentaire