ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Java Program Examples in Netbeans: Palindrome Test Java Source Code

Updated on February 26, 2013

Java Program: How to make a Palindrome Test in Java? Sample Java source code is given.

Recently, we just finished the reverse string java program. We will use this program as a basis to have a Palindrome test program. If you do not know about Palindrome, it is a word or a group of word that reads and looks exactly the same as the original text when reversed. For example, the famous Palindrome word is ma’am, so whether you read it from first letter to last letter or from last letter to first letter, the word is still the same. Therefore, the word ma’am is a palindrome. So, in this program we will test each string of words entered by the user if it is a Palindrome or Not and print the result on the screen.

Java Source Code for Palindrome Test

// main class



package ispalindrome;

import java.util.Scanner;

public class Main {

    public static void main(String[] args)
    {
		Scanner Myscan = new Scanner(System.in);

	        String word = "";
	        System.out.print("Enter a String: ");
	        word = Myscan.nextLine();
	        is_Palindrome access = new is_Palindrome();


	        if(access.isPalindrome(word) == true)
	            System.out.println(word + " is a PALINDROME.");
	        else
	            System.out.println(word + " is NOT A PALINDROME.");
    }

}


//java class


package ispalindrome;

public class is_Palindrome
{

    public boolean isPalindrome(String word_entered)
    {
        if(word_entered.length() == 1 )
            return true;
        else if (word_entered.length()==2)
        {
            if(word_entered.charAt(0) == word_entered.charAt(word_entered.length()-1))
            {
            	return true;
            }
        }

        else
        {
        	 if(word_entered.charAt(0) == word_entered.charAt(word_entered.length() - 1))
                 {
                    return isPalindrome(word_entered.substring(1, word_entered.length() - 1));
                 }
                else
                    return false;
        }
        	return false;
}
}

Sample Output:

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      zampano 4 years ago

      Very nice.

      I would solve the problem with a plain brute loop parsing the string from backwards and breaking at the first non match.

      But your recursive call solution is rather elegant.

      Bonne suite.

    • aisha91 profile image
      Author

      Rasna Aisha 4 years ago from Manila, Philippines

      Hi zampano,

      Parsing the string backward for Palindrome test is also good. Thanks for sharing it.

      Bonne Suite too :D, haha I look for that words but didn't see the exact translation. I just perceive it as "happy programming!". :D Good day to you zampano.

    • profile image

      zampano 4 years ago

      It's about what it means.

      Bonne suite extends All_Good_to_Follow

    • aisha91 profile image
      Author

      Rasna Aisha 4 years ago from Manila, Philippines

      yay, thanks for commenting back. I didn't expect it. Actually I am just joking about the 'happy programming' translation. I heard Bonne Suite a few times, especially when someone bid goodbye, I know now what it means, thanks to you.

    Click to Rate This Article