Question:Determine whether an integer is a palindrome. Do this without extra space.
/************************************************************************************/
package Palidrome;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Palidrome {
public static void main(String[] args) throws NumberFormatException, IOException{
int num;
boolean result;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please input a integer number, and this programe will " +
"check whether this number is a palidrome.");
num = (new Integer(stdin.readLine()));
result = isPalindrome(num);
System.out.println("This integer number is a palindrome ? ");
System.out.println(result);
}
//方法
public static boolean isPalindrome(int num) {
//将整形数字num转化为一个名为s的字符串
String s = Integer.toString(num);
//将字符串转化为一个名为charNum的字符数组
char[] charNum = s.toCharArray();
int i = 0;
int j = charNum.length - 1;
char temp;
//设置表置位,并初始化为false。当symbol = true时,输入数字为palindrome,否则不是。
boolean symbol = false;
if(charNum.length <= 1){
return true;
}
if(num <= 0 || num >= 2147483647){
return false;
}
while(i < j){
temp = charNum[i];
charNum[i] = charNum[j];
charNum[j] = temp;
i++;
j--;
}
//将字符数组合并为一个新的字符串
String resultString = new String(charNum);
//将字符串转化为一个整形数
Double resultNum = Double.parseDouble(resultString);
if(resultNum == num){
symbol = true;
}
else{
symbol = false;
}
return symbol;
}
}