Palindrome in Java GUI

Submitted by: 
Language: 
Visitors have accessed this post 3603 times.

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.