smd
Source Code Analyzer
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
Sample run#1:
sample run#2
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.
To start in this tutorial, first open the JCreator IDE, click new and paste the following code
- import
javax.swing.JOptionPane
;
- public
class
Palindrome
- {
- public
static
void
main(
String
[
]
args)
- {
- long
num;
- long
temp;
- String
inputStr;
- String
outputStr;
- inputStr =
- JOptionPane
.showInputDialog
(
"Enter an integer, "
- +
"positive or negative"
)
;
- num =
Long
.parseLong
(
inputStr)
;
- temp =
num;
- if
(
num <=
0
)
- {
- num =
-
num;
- inputStr =
inputStr.valueOf
(
num)
;
- }
- if
(
isPalindrome (
inputStr)
)
- outputStr =
temp +
" is a palindrome"
;
- else
- outputStr =
temp +
"is not a palindrome"
;
- JOptionPane
.showMessageDialog
(
null
, outputStr,
- "Palindrome Program"
, JOptionPane
.INFORMATION_MESSAGE
)
;
- System
.exit
(
0
)
;
- }
- public
static
boolean
isPalindrome(
String
str)
- {
- int
len =
str.length
(
)
;
- int
i, j;
- j =
len -
1
;
- for
(
i =
0
;
i <=
(
len -
1
)
/
2
;
i++
)
- {
- if
(
str.charAt
(
i)
!=
str.charAt
(
j)
)
- return
false
;
- j--;
- }
- return
true
;
- }
- }
Sample run#1:


sample run#2


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.