【发布时间】:2018-07-04 22:54:38
【问题描述】:
我在 Java 中编写了一些代码,查看被归为字符串的内容,然后将该字符串转换为 double 或 int IF 找到一个小数(句点)。在我的代码中,它总是跳出循环并打印“找不到数字”。这是为什么?怎么了。
我的代码:
//check if its actually numbers
if(leftSide.matches("[0-9]+") && rightSide.matches("[0-9]+")){
System.out.println("numbers found");
if(leftSide.contains(".") || rightSide.contains(".")){
System.out.println("convert to doubles");
}else{
System.out.println("convert to ints");
}
}else{
System.out.println("no numbers found");
}
【问题讨论】:
-
你为什么不用try/catch来做呢?
-
尝试调试您的代码?
String.matches也将评估整个字符串。 -
请阅读此帮助页面:stackoverflow.com/help/mcve
-
leftSide和rightSide是如何实例化的? -
这部分代码:if(leftSide.matches("[0-9]+") && rightSide.matches("[0-9]+")) 与下面的相矛盾: if(leftSide.contains(".") || rightSide.contains(".")){
标签: java string if-statement int double