Java 函数来标识值是否可以转换为int、double或其他形式?
如果有一个字符串变量包含一个数字字符串,是否有任何函数来标识该值是否可以转换为int、double或其他值??我需要java中的函数名Java 函数来标识值是否可以转换为int、double或其他形式?,java,Java,如果有一个字符串变量包含一个数字字符串,是否有任何函数来标识该值是否可以转换为int、double或其他值??我需要java中的函数名 String sent3 = "123"; System.out.println(sent3.matches("[0-9]+")); System.out.println(sent3.matches("[0-9]+\\.[0-9]+"));// for double 输出:-正确 如果输出为true,则可以将其转换为int 输出:-正确 如果输出为tru
String sent3 = "123";
System.out.println(sent3.matches("[0-9]+"));
System.out.println(sent3.matches("[0-9]+\\.[0-9]+"));// for double
输出:-正确
如果输出为true
,则可以将其转换为int
输出:-正确
如果输出为true
,则可以将其转换为int
输出:-正确
如果输出为true
,则可以将其转换为int
输出:-正确
如果输出为true
,则可以将其转换为int
第一个匹配(即打印true)任何整数(不是int
,整数),前面有一个可选的-
符号。第二个值将任何双精度
值与可选的-
符号相匹配,在所需小数点之前至少有一个数字,在小数点之后至少有一个数字
此外,函数名为String.matches
,并使用正则表达式
第一个匹配(即打印true)任何整数(不是int
,整数),前面有一个可选的-
符号。第二个值将任何双精度
值与可选的-
符号相匹配,在所需小数点之前至少有一个数字,在小数点之后至少有一个数字
此外,函数名为String.matches
,并使用正则表达式
第一个匹配(即打印true)任何整数(不是int
,整数),前面有一个可选的-
符号。第二个值将任何双精度
值与可选的-
符号相匹配,在所需小数点之前至少有一个数字,在小数点之后至少有一个数字
此外,函数名为String.matches
,并使用正则表达式
第一个匹配(即打印true)任何整数(不是int
,整数),前面有一个可选的-
符号。第二个值将任何双精度
值与可选的-
符号相匹配,在所需小数点之前至少有一个数字,在小数点之后至少有一个数字
另外,函数名是
String.matches
,它使用正则表达式。我的解决方案包括尝试将字符串解析为各种类型,然后查找Java可能抛出的异常。这可能是一个低效的解决方案,但代码相对较短
public static Object convert(String tmp)
{
Object i;
try {
i = Integer.parseInt(tmp);
} catch (Exception e) {
try {
i = Double.parseDouble(tmp);
} catch (Exception p) {
return tmp; // a number format exception was thrown when trying to parse as an integer and as a double, so it can only be a string
}
return i; // a number format exception was thrown when trying to parse an integer, but none was thrown when trying to parse as a double, so it is a double
}
return i; // no numberformatexception was thrown so it is an integer
}
然后,您可以将此函数与以下代码行一起使用:
String tmp = "3"; // or "India" or "3.14"
Object tmp2 = convert(tmp);
System.out.println(tmp2.getClass().getName());
您可以将函数转换为内联代码以测试它是否为整数,例如:
String tmp = "3";
Object i = tmp;
try {
i = Integer.parseInt(tmp);
} catch (Exception e) {
// do nothing
}
我有点草率,试图捕捉正常的异常,这是相当普遍的-我建议您改用“NumberFormatException”。我的解决方案包括尝试将字符串解析为各种类型,然后查找Java可能抛出的异常。这可能是一个低效的解决方案,但代码相对较短
public static Object convert(String tmp)
{
Object i;
try {
i = Integer.parseInt(tmp);
} catch (Exception e) {
try {
i = Double.parseDouble(tmp);
} catch (Exception p) {
return tmp; // a number format exception was thrown when trying to parse as an integer and as a double, so it can only be a string
}
return i; // a number format exception was thrown when trying to parse an integer, but none was thrown when trying to parse as a double, so it is a double
}
return i; // no numberformatexception was thrown so it is an integer
}
然后,您可以将此函数与以下代码行一起使用:
String tmp = "3"; // or "India" or "3.14"
Object tmp2 = convert(tmp);
System.out.println(tmp2.getClass().getName());
您可以将函数转换为内联代码以测试它是否为整数,例如:
String tmp = "3";
Object i = tmp;
try {
i = Integer.parseInt(tmp);
} catch (Exception e) {
// do nothing
}
我有点草率,试图捕捉正常的异常,这是相当普遍的-我建议您改用“NumberFormatException”。我的解决方案包括尝试将字符串解析为各种类型,然后查找Java可能抛出的异常。这可能是一个低效的解决方案,但代码相对较短
public static Object convert(String tmp)
{
Object i;
try {
i = Integer.parseInt(tmp);
} catch (Exception e) {
try {
i = Double.parseDouble(tmp);
} catch (Exception p) {
return tmp; // a number format exception was thrown when trying to parse as an integer and as a double, so it can only be a string
}
return i; // a number format exception was thrown when trying to parse an integer, but none was thrown when trying to parse as a double, so it is a double
}
return i; // no numberformatexception was thrown so it is an integer
}
然后,您可以将此函数与以下代码行一起使用:
String tmp = "3"; // or "India" or "3.14"
Object tmp2 = convert(tmp);
System.out.println(tmp2.getClass().getName());
您可以将函数转换为内联代码以测试它是否为整数,例如:
String tmp = "3";
Object i = tmp;
try {
i = Integer.parseInt(tmp);
} catch (Exception e) {
// do nothing
}
我有点草率,试图捕捉正常的异常,这是相当普遍的-我建议您改用“NumberFormatException”。我的解决方案包括尝试将字符串解析为各种类型,然后查找Java可能抛出的异常。这可能是一个低效的解决方案,但代码相对较短
public static Object convert(String tmp)
{
Object i;
try {
i = Integer.parseInt(tmp);
} catch (Exception e) {
try {
i = Double.parseDouble(tmp);
} catch (Exception p) {
return tmp; // a number format exception was thrown when trying to parse as an integer and as a double, so it can only be a string
}
return i; // a number format exception was thrown when trying to parse an integer, but none was thrown when trying to parse as a double, so it is a double
}
return i; // no numberformatexception was thrown so it is an integer
}
然后,您可以将此函数与以下代码行一起使用:
String tmp = "3"; // or "India" or "3.14"
Object tmp2 = convert(tmp);
System.out.println(tmp2.getClass().getName());
您可以将函数转换为内联代码以测试它是否为整数,例如:
String tmp = "3";
Object i = tmp;
try {
i = Integer.parseInt(tmp);
} catch (Exception e) {
// do nothing
}
我有点草率,试图捕捉正常的异常,这是相当普遍的-我建议您改用“NumberFormatException”。我有点困惑,这不是在测试数字是否只包含数字和小数点吗?@Bucco if(变量==“India”)然后转换为字符串if(变量==“562”)然后转换为int if(变量==“54.3354”)然后转换为double我有点困惑,这不是在测试数字是否只包含数字和小数点吗?@Bucco if(变量==“India”)然后转换为字符串if(变量==“562”)然后转换为int if(变量==“54.3354”)然后转换为double我有点困惑,这不是在测试数字是否只包含数字和小数点吗?@Bucco if(变量==“India”)然后转换为字符串if(变量==“562”)然后转换为int if(变量==“54.3354”)然后转换为double我有点困惑,这不是在测试数字是否只包含数字,小数点?@Bucco if(变量==“India”)然后转换为字符串if(变量==“562”),然后转换为int if(变量==“54.3354”),然后转换为doubleFalse。当
sent3=“123456789000”
无法转换为int
时匹配。此外,您的正则表达式不处理打印为true的-
签名。我是说你的陈述是假的。尝试将“123456789000”解析为int
。其值太大(大于Inte