public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //从键盘输入字符串 String str = scanner.nextLine(); System.out.println(isNumber(str)); } public static boolean isNumber(String str) { for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (!('0' <= c && c <= '9' || c == '.')) return false; } return true; } }
public class zhu { public static boolean test(String s) { try { Integer i = new Integer(s); return i.intValue() > 0; } catch (Exception e) { System.out.println("not a integer"); return false; } } public static void main(String[] args) { if(test("123a45") == true) System.out.println("Yes"); } }
String s = "adjk212"; Pattern p = Pattern.compile("[1-9]+"); Matcher m = p.matcher(s); System.out.println(m.matches());
public class TestString { public static void main(String[] args) { String ss = "-1sfd234567895143216465156465135465156465416"; System.out.println(new TestString().isNum(ss)); } public boolean isNum(String s) { boolean flag = false; int length = s.length(); for(int i=1; i<length; i++) { char cc = s.charAt(0); char c = s.charAt(i); if(cc == '-') { flag = true; } if(c<'0' || c>'9') { return false; } } if(flag) { System.out.println("is negative number"); } return true; }}
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Number { public static void main(String[] args) { String str = "121212300010020122"; Pattern p = Pattern.compile("[1-9]\\d*");//正则表达式是这样的 Matcher m = p.matcher(str); System.out.println(m.matches()); } }上面的一位写的正则,有问题,因为没数字中没有包括0
public class NumberHelper { // 仅检测 10进制数字 public static boolean isPositive(String number){ String numberc = number.trim(); if(numberc.matches("^[1-9]\\d*$") || numberc.matches("^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$")) return true; return false; } public static void main(final String[] args) { System.out.println(isPositive("0")); System.out.println(isPositive("-1")); System.out.println(isPositive("3232")); System.out.println(isPositive("3.232")); System.out.println(isPositive("32ad")); } }
#2楼没有考虑小数、大数的情况。 public class zhu { public static boolean test(String s) { try { BigDecimal i = BigDecimal.valueOf(s); return i > 0; } catch (Exception e) { System.out.println("not a number"); return false; } } public static void main(String[] args) { if(test("123a45") == true) System.out.println("Yes"); } }
import java.util.*;public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); //从键盘输入字符串
String str = scanner.nextLine();
System.out.println(isNumber(str));
} public static boolean isNumber(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!('0' <= c && c <= '9' || c == '.'))
return false;
}
return true;
}
}
public class zhu {
public static boolean test(String s)
{
try {
Integer i = new Integer(s);
return i.intValue() > 0;
} catch (Exception e) {
System.out.println("not a integer");
return false;
}
}
public static void main(String[] args) {
if(test("123a45") == true)
System.out.println("Yes");
}
}
Pattern p = Pattern.compile("[1-9]+");
Matcher m = p.matcher(s);
System.out.println(m.matches());
public static void main(String[] args) {
String ss = "-1sfd234567895143216465156465135465156465416";
System.out.println(new TestString().isNum(ss));
}
public boolean isNum(String s) {
boolean flag = false;
int length = s.length();
for(int i=1; i<length; i++) {
char cc = s.charAt(0);
char c = s.charAt(i);
if(cc == '-') {
flag = true;
}
if(c<'0' || c>'9') {
return false;
}
}
if(flag) {
System.out.println("is negative number");
}
return true;
}}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Number {
public static void main(String[] args) {
String str = "121212300010020122";
Pattern p = Pattern.compile("[1-9]\\d*");//正则表达式是这样的
Matcher m = p.matcher(str);
System.out.println(m.matches());
}
}上面的一位写的正则,有问题,因为没数字中没有包括0
// 仅检测 10进制数字
public static boolean isPositive(String number){
String numberc = number.trim();
if(numberc.matches("^[1-9]\\d*$") || numberc.matches("^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$")) return true;
return false;
}
public static void main(final String[] args) {
System.out.println(isPositive("0"));
System.out.println(isPositive("-1"));
System.out.println(isPositive("3232"));
System.out.println(isPositive("3.232"));
System.out.println(isPositive("32ad"));
}
}
public class zhu {
public static boolean test(String s)
{
try {
BigDecimal i = BigDecimal.valueOf(s);
return i > 0;
} catch (Exception e) {
System.out.println("not a number");
return false;
}
}
public static void main(String[] args) {
if(test("123a45") == true)
System.out.println("Yes");
}
}
public static boolean isNumber(String str) {
try {
Double d = Double.parseDouble(str);
if (d <= 0)
return false;
} catch (NumberFormatException e) {
return false;
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(isNumber(scanner.nextLine()));
}
}
(2)Double.parseDouble()方法推荐Double.parseDouble()拿分了哦