Skip to main content

Palindrome in Java GUI


We all know that palindrome is a word or phrase that reads the same backward as forward. Here in this tutorial, we will create a program that can determine if the word inputted is a palindrome or not.

Now, let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of palindrome.java.

2. Import javax.swing package. Hence we will use a GUI (Graphical User Interface) here like the inputting the word.

  1. import javax.swing.*;

3. Before coding in the Main, create first a function named isPalindrome as Boolean that has variable word as string as parameter of it.

  1. public static boolean isPalindrome(String word) {
  2. int left = 0; // index of leftmost unchecked char
  3. int right = word.length() -1; // index of the rightmost
  4.  
  5. while (left < right) { // continue until they reach center
  6. if (word.charAt(left) != word.charAt(right)) {
  7. return false; // if chars are different, finished
  8. }
  9. left++; // move left index toward the center
  10. right--; // move right index toward the center
  11. }
  12.  
  13. return true; // if finished, all chars were same
  14. }

4. Initialized your variable in your Main, variable Str as string and make it as an input.

  1. String Str;
  2. Str = JOptionPane.showInputDialog(null, "Enter String:");

5. Lastly, display the output of the program whether the word is a palindrome or not.

  1. JOptionPane.showMessageDialog(null,"The word " + Str + " is palindrome: " + isPalindrome(Str) );

Take note that isPalindrome(Str) syntax triggers to access the boolean function that will validate the inputted word of variable Str.

Output:

output


Here's the full code of this tutorial:

  1. import javax.swing.*;
  2.  
  3. public class palindrome {
  4.  
  5.  
  6. public static void main(String[] args) {
  7.  
  8. String Str;
  9.  
  10. Str = JOptionPane.showInputDialog(null, "Enter String:");
  11.  
  12. JOptionPane.showMessageDialog(null,"The word " + Str + " is palindrome: " + isPalindrome(Str) );
  13. }
  14. public static boolean isPalindrome(String word) {
  15. int left = 0;
  16. int right = word.length() -1;
  17.  
  18. while (left < right) {
  19. if (word.charAt(left) != word.charAt(right)) {
  20. return false;
  21. }
  22. left++;
  23. right--;
  24. }
  25.  
  26. return true;
  27. }
  28. }

Hope this helps! :)

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.