zoukankan      html  css  js  c++  java
  • Valid Number,判断是否为合法数字

    问题描述:

    Validate if a given string is numeric.

    Some examples:
    "0" => true
    " 0.1 " => true
    "abc" => false
    "1 a" => false
    "2e10" => true

    public boolean isNumber(String s)
    	{
            s = s.trim();
            if (s.length() == 0) return false;
            boolean hasE = false;
            boolean hasDot = false;
            boolean hasNumber = false;
            
            for (int i = 0; i < s.length(); i++) 
            {
                // e cannot be the first character
                if (i == 0 && s.charAt(i) == 'e') return false;
                if (s.charAt(i) == 'e') {
                    // e cannot be replicated nor placed before number
                    if (hasE == true || hasNumber == false) {
                        return false;
                    } else {
                    	hasE = true;
                    }
                } 
                
                if (s.charAt(i) == '.') {
                    // '.' cannot be replicated nor placed after 'e'
                	if (hasDot == true || hasE == true) {
                    	return false;
                    } else {
                    	hasDot = true;
                    }
                }
                // the sign can be placed at the beginning or after 'e'
                if (i != 0 && s.charAt(i - 1) != 'e' && (s.charAt(i) == '+' || s.charAt(i) == '-')) return false;
                
                // no other chacraters except '+', '-', '.', and 'e'
                if ((s.charAt(i) > '9' || s.charAt(i) < '0') && s.charAt(i) != '+' && s.charAt(i) != '-' && s.charAt(i) != '.' && s.charAt(i) != 'e')
                return false;  
                
                // check whether numbers are included.
                if (s.charAt(i) <= '9' && s.charAt(i) >= '0')
                {
                    hasNumber = true;
                }
            }
            // '+', '-', and 'e' cannot be the last character
            if (s.charAt(s.length() - 1) == '-' || s.charAt(s.length() - 1) == '+' || s.charAt(s.length() - 1) == 'e') return false;
    
            return hasNumber;
        }
    }
    
  • 相关阅读:
    java占位符
    linux安装jdk
    linux安装svn
    java判断是汉字和英文
    mysql删除未提交的事务
    Html 解决长串英文字母显示不能自动换行
    spring boot 定时任务
    mybatis批量插入数据
    文本域换行符号
    rabbitmq,生成者和消费者
  • 原文地址:https://www.cnblogs.com/masterlibin/p/5776754.html
Copyright © 2011-2022 走看看