提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
检查字符串是否包含有效数字
public class Main { public static void main(String[] args) {/**来 自 时代Java公众号 - nowjava.com**/ String[] str = new String[] { "10.20", "1234.56", "12.invalid","1.1.1" }; for (int i = 0; i < str.length; i++) { if (str[i].indexOf(".") > 0) { try { Double.parseDouble(str[i]); System.out.println(str[i] + " is a valid decimal number"); } catch (NumberFormatException nme) { System.out.println(str[i] + " is not a valid decimal number"); } } else { try { /* from N o w J a v a . c o m*/ Integer.parseInt(str[i]); System.out.println(str[i] +