怎么判断一个字符串是数字并且是正数 

解决方案 »

  1.   

    一个字一个字的判断
    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;
    }
    }
      

  2.   


    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");
    }
    }
      

  3.   

    String s = "adjk212";
    Pattern p = Pattern.compile("[1-9]+");
    Matcher m = p.matcher(s);
    System.out.println(m.matches());
      

  4.   

    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;
    }}
      

  5.   


    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
      

  6.   

    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"));
        }
    }
      

  7.   

    #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");
        }
    }
      

  8.   

    直接使用Interger.parseInt().如果没有异常就是数值,自己捕获一下异常。最简单的方法
      

  9.   

    还是用异常好import java.util.*;public class Test {

    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()));
    }
    }
      

  10.   

    (1)正则表达式
    (2)Double.parseDouble()方法推荐Double.parseDouble()拿分了哦