java中有没有方法判断某个数是不是整数,且返回布尔型?

例如这个函数叫iszhenshu(),则iszhenshu(3.122)返回flase
有这样的方法么?谢谢

//判断是否为整型数字
public boolean isNum(String s) {
for (int index = 0; index < s.length(); index++) {
if (48 > (int) s.charAt(index) || (int) s.charAt(index) > 57) {
return false;
}
}
return true;
}
因为你没有说你要输入的数是什么类型,所以在这个方法之前把你输入的类型先转为String型
温馨提示:内容为网友见解,仅供参考
第1个回答  2006-09-21
上面两函数分别各有两种方法可以实现

一、一个个字符判断下去(效率高些)
下面的 iisNumeric(String)、isInteger(String)

二、利用异常:用Integer.parseInt(str),Double.parseDouble(str)解析字符串,若非数字则抛出异常
下面的 isNumericEx(String)、isIntegerEx(String)--其中isIntegerEx(String)最多支持到十位

package hartech;

/**
* <p>Copyright: Copyright (c) 2006 hartech.cn</p>
*
* <p>Website: www.hartech.cn</p>
*
* <p>Page: http://www.hartech.cn/blog/blogview.asp?logID=73 </p>
*
* @author JTL.zheng@gmail.com
* @version 1.0
*/
public class JMath {

/**
* support Numeric format:<br>
* "33" "+33" "033.30" "-.33" ".33" " 33." " 000.000 "
* @param str String
* @return boolean
*/
public static boolean isNumeric(String str) {
int begin = 0;
boolean once = true;
if (str == null || str.trim().equals("")) {
return false;
}
str = str.trim();
if (str.startsWith("+") || str.startsWith("-")) {
if (str.length() == 1) {
// "+" "-"
return false;
}
begin = 1;
}
for (int i = begin; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
if (str.charAt(i) == '.' && once) {
// '.' can only once
once = false;
}
else {
return false;
}
}
}
if (str.length() == (begin + 1) && !once) {
// "." "+." "-."
return false;
}
return true;
}

/**
* support Integer format:<br>
* "33" "003300" "+33" " -0000 "
* @param str String
* @return boolean
*/
public static boolean isInteger(String str) {
int begin = 0;
if (str == null || str.trim().equals("")) {
return false;
}
str = str.trim();
if (str.startsWith("+") || str.startsWith("-")) {
if (str.length() == 1) {
// "+" "-"
return false;
}
begin = 1;
}
for (int i = begin; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}

/**
* use Exception
* support Numeric format:<br>
* "33" "+33" "033.30" "-.33" ".33" " 33." " 000.000 "
* @param str String
* @return boolean
*/
public static boolean isNumericEx(String str) {
try {
Double.parseDouble(str);
return true;
}
catch (NumberFormatException ex) {
return false;
}
}

/**
* use Exception
* support less than 11 digits(<11)<br>
* support Integer format:<br>
* "33" "003300" "+33" " -0000 " "+ 000"
* @param str String
* @return boolean
*/
public static boolean isIntegerEx(String str) {
str = str.trim();
try {
Integer.parseInt(str);
return true;
}
catch (NumberFormatException ex) {
if (str.startsWith("+")) {
return isIntegerEx(str.substring(1));
}
return false;
}
}
}
第2个回答  2006-09-21
太麻烦了,用正则表达式很简单,先将你传的对象转成String对象然后调用下面的方法就行了.
pulbic boolean isInteger(String input){
if (input == null || "".equals(input))
return false ;
if ( input.toString().matches("[0-9]{1,}"))
return true ;
else
return false ;
}
第3个回答  2006-09-22
把输入的内容转成String,加上异常处理,如果不是数字的话则抛出异常。
如果是数字的话那就很简单了,数字中如果不是整数,那必然是小数或者是那种科学表达式,不管是什么,中间都有小数点的,所以只要判断String里有没有小数点就可以了。
相似回答