test
2015-06-12 11:31
204 查看
package com.ebay.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public
class StringUtil {
public
static
void main(String[] args) {
try {
int result = StringUtil.stringToInt("-1000.01");
System.out.println(result);
} catch (NumFmtException e) {
e.printStackTrace();
} catch (NumExcessException e) {
e.printStackTrace();
}
}
public
static
int stringToInt(String str)
throws NumFmtException, NumExcessException {
int result = 0;
//preProcess string
String tmpStr = preProcess(str);
if (tmpStr.equals(""))
{
return 0;
}
if(isLegalInt(tmpStr)) {
System.out.println("is
legalInt");
result = process(tmpStr);
} else {
throw
new NumFmtException("string
must match ^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0|1-9]\\d*)$");
}
return result;
}
/**
* check if the string match regex
*
@param str
*
@return
*/
private
static
boolean isLegalInt(String str) {
Pattern p = Pattern
.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0|[1-9]\\d*)$");
Matcher m = p.matcher(str);
if (m.matches()) {
return
true;
} else {
return
false;
}
}
/**
* trim the string
*
*
@param str
*
@return
*/
private
static String preProcess(String str) {
if(str ==
null) {
return
"";
}
return str.trim();
}
private
static
int process(String str)
throws NumExcessException {
//remove '.' and negative sign
boolean isNegative =
false;
int result = 0;
String tmpStr = "";
if(str.contains("."))
{
tmpStr = str.substring(0, str.indexOf("."));
}
if (tmpStr.startsWith("-"))
{
isNegative = true;
tmpStr = tmpStr.substring(1, tmpStr.length());
}
//convert to long first
char[] chs = tmpStr.toCharArray();
long l = 0l;
for (int
i = chs.length-1, j =0; i >= 0; i--, j++) {
l += (chs[i]-'0') * Math.pow(10, j);
}
//check value range
if (l>Integer.MAX_VALUE)
{
throw
new NumExcessException("string
is excess integer max value");
}
result = (int)l;
if (isNegative) {
result *= -1;
}
return result;
}
/**
* validate the string
*
*
@param str
*
@return
*/
private
static
boolean
validate(String str) {
return
true;
}
static
class
NumFmtException extends Exception {
public NumFmtException(String message) {
super(message);
}
}
static
class
NumExcessException extends Exception {
public NumExcessException(String message) {
super(message);
}
}
}
package com.ebay.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public
class StringUtil {
public
static
void main(String[] args) {
try {
int result = StringUtil.stringToInt("-1000.01");
System.out.println(result);
} catch (NumFmtException e) {
e.printStackTrace();
} catch (NumExcessException e) {
e.printStackTrace();
}
}
public
static
int stringToInt(String str)
throws NumFmtException, NumExcessException {
int result = 0;
//preProcess string
String tmpStr = preProcess(str);
if (tmpStr.equals(""))
{
return 0;
}
if(isLegalInt(tmpStr)) {
System.out.println("is
legalInt");
result = process(tmpStr);
} else {
throw
new NumFmtException("string
must match ^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0|1-9]\\d*)$");
}
return result;
}
/**
* check if the string match regex
*
@param str
*
@return
*/
private
static
boolean isLegalInt(String str) {
Pattern p = Pattern
.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0|[1-9]\\d*)$");
Matcher m = p.matcher(str);
if (m.matches()) {
return
true;
} else {
return
false;
}
}
/**
* trim the string
*
*
@param str
*
@return
*/
private
static String preProcess(String str) {
if(str ==
null) {
return
"";
}
return str.trim();
}
private
static
int process(String str)
throws NumExcessException {
//remove '.' and negative sign
boolean isNegative =
false;
int result = 0;
String tmpStr = "";
if(str.contains("."))
{
tmpStr = str.substring(0, str.indexOf("."));
}
if (tmpStr.startsWith("-"))
{
isNegative = true;
tmpStr = tmpStr.substring(1, tmpStr.length());
}
//convert to long first
char[] chs = tmpStr.toCharArray();
long l = 0l;
for (int
i = chs.length-1, j =0; i >= 0; i--, j++) {
l += (chs[i]-'0') * Math.pow(10, j);
}
//check value range
if (l>Integer.MAX_VALUE)
{
throw
new NumExcessException("string
is excess integer max value");
}
result = (int)l;
if (isNegative) {
result *= -1;
}
return result;
}
/**
* validate the string
*
*
@param str
*
@return
*/
private
static
boolean
validate(String str) {
return
true;
}
static
class
NumFmtException extends Exception {
public NumFmtException(String message) {
super(message);
}
}
static
class
NumExcessException extends Exception {
public NumExcessException(String message) {
super(message);
}
}
}
相关文章推荐
- Oracle中定义package以及存储过程的使用
- FileUpload类别FileUpload1.FileName和FileUpload1.PostedFile.FileName差异
- 微信支付开发关键点技术解析
- 配置HistoryServer
- 几种垃圾回收机制
- Java 中 modifer 'public' is reduntant for interface methods
- 我与学院的点点滴滴
- MS-SqlServer将本地数据库更新,定时同步到外网数据库
- 高通msm8994启动流程简介
- javabean总结
- @media 适配兼容
- 深圳华瑞三和集团有限公司携手泛微华南大区
- quartz CronExpression表达式
- Openwrt 串口多用户访问
- 操作系统宏定义
- C++ string 字符串 知识点 小结
- Android淘宝电影日期滚动栏的实现
- 构建科技初创企业生态系统 中国应向新加坡学习
- xUtils框架
- 泛微OA体验模式给行业带来冲击