• We just launched and are currently in beta. Join us as we build and grow the community.

Palindrome Number in Java Application

smd

Source Code Analyzer
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
84
Likes
46
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
The following Java program application is a Palindrome Number. An integer is a Palindrome if it reads forward and backward in the same way, ignoring any leading minus sign. In this program, we use an input dialog box to get the input and an output dialog box to show the output. I will be using the JCreator IDE in developing the program.

To start in this tutorial, first open the JCreator IDE, click new and paste the following code

  1. import

    javax.swing.JOptionPane

    ;

  2. public

    class

    Palindrome
  3. {
  4. public

    static

    void

    main(

    String

    [

    ]

    args)
  5. {
  6. long

    num;
  7. long

    temp;

  8. String

    inputStr;
  9. String

    outputStr;

  10. inputStr =
  11. JOptionPane

    .showInputDialog

    (

    "Enter an integer, "
  12. +

    "positive or negative"

    )

    ;

  13. num =

    Long

    .parseLong

    (

    inputStr)

    ;
  14. temp =

    num;

  15. if

    (

    num <=

    0

    )
  16. {
  17. num =

    -

    num;
  18. inputStr =

    inputStr.valueOf

    (

    num)

    ;

  19. }
  20. if

    (

    isPalindrome (

    inputStr)

    )
  21. outputStr =

    temp +

    " is a palindrome"

    ;
  22. else
  23. outputStr =

    temp +

    "is not a palindrome"

    ;

  24. JOptionPane

    .showMessageDialog

    (

    null

    , outputStr,
  25. "Palindrome Program"

    , JOptionPane

    .INFORMATION_MESSAGE

    )

    ;
  26. System

    .exit

    (

    0

    )

    ;
  27. }
  28. public

    static

    boolean

    isPalindrome(

    String

    str)
  29. {
  30. int

    len =

    str.length

    (

    )

    ;
  31. int

    i, j;

  32. j =

    len -

    1

    ;

  33. for

    (

    i =

    0

    ;

    i <=

    (

    len -

    1

    )

    /

    2

    ;

    i++

    )
  34. {
  35. if

    (

    str.charAt

    (

    i)

    !=

    str.charAt

    (

    j)

    )
  36. return

    false

    ;
  37. j--;
  38. }
  39. return

    true

    ;

  40. }
  41. }

Sample run#1:

pal1_0.png

pal2.png


sample run#2
pal3.png

pal4.png


The program algorithm are the following:

The first thing that the program does is read the integer.

Because the input of the program is string, the string containing the integer is converted into the integer.

If the integer is negative, the program changes it to positive.

The next step is to convert the integer back to a string and then determine whether the string is palindrome.

 

452,292

323,526

323,535

Top