-2

Trying to get the console output to tell me if a word is a palindrome
I am fairly new to java so this may be a simple solution but I am not sure how to do it myself.

package checkpalindrome;
import java.io.*;

public class CheckPalindrome 
{
    public static void main(String[] args)  throws Exception
    {
        String input;
        System.out.println("Enter a word");
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            input = br.readLine();
            char[]try1=input.toCharArray();
            for (int i=try1.length-1;i>=0;i--)
                if (String.valueOf(try1)==input)
                {
                    System.out.println(input +" is a Palindrome");
                }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }        
    }
} 
khelwood
  • 52,115
  • 13
  • 74
  • 94
Jakebrady
  • 17
  • 1
  • 2
  • 3
    There are so many examples of how to detect palindromes in Java, even here on SO. Did you have a look at those already? – Thomas Jul 18 '17 at 13:53
  • @Jakebrady what exactly is your question? – jps Jul 18 '17 at 13:53
  • ONe hint though: `String.valueOf(try1)==input` - _never_ compare strings using `==` as you'll only get true if both strings are the same instance. Use `String.valueOf(try1).equals( input )` instead (the same holds for all other objects in Java). – Thomas Jul 18 '17 at 13:55
  • Vote to close, there is dozens of "check palindrome" in Java on SO – azro Jul 18 '17 at 13:56
  • Double check what you're doing around your for loop: You're changing a `String` to a `char[]` then comparing your `String` and this `char[]`. This doesn't make sense. – Turtle Jul 18 '17 at 13:57
  • Ready code [HERE](http://www.programmingsimplified.com/java/source-code/java-program-check-palindrome) – Frank Jul 18 '17 at 14:06

1 Answers1

-1

Here is an Easy Solution with Scanner class.

class TestClass {
public static void main(String args[] ) throws Exception {


    java.util.Scanner kb=new java.util.Scanner(System.in);
    String rev="";
    String s=kb.next();
    int length=s.length();
    for(int i=length-1; i>=0;i--)
    {
        rev=rev+s.charAt(i);
    }
    if(rev.equals(s))
    System.out.println("YES  " + rev);
    else
    System.out.println("NO");
}
}
Sameer Reza Khan
  • 452
  • 4
  • 14