首页 > 其他分享 >test

test

时间:2023-07-03 12:32:36浏览次数:19  
标签:return String static result str test tmpStr

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

标签:return,String,static,result,str,test,tmpStr
From: https://blog.51cto.com/u_16174476/6610413

相关文章

  • tree-test
    #include<iostream>#include<stack>usingnamespacestd;typedefstructBiTNode{ chardata; structBiTNode*lchild; structBiTNode*rchild;}BiTNode,*BiTree;voidCreateTree(BiTree*Tree){ charch; ch=getchar(); if(ch=='.')......
  • AtCoder Beginner Contest 308 G Minimum Xor Pair Query
    洛谷传送门AtCoder传送门考虑没有删除操作怎么做。可以动态维护\(ans\),新加进来一个\(x\),我们计算\(\min\limits_{y\inS}x\oplusy\)。对\(S\)建01Trie,然后从高位往低位按位贪心,在01Trie上优先往与\(x\)这一位相同的方向走,但是前提是它的子树内有数,对于01Trie......
  • AtCoder Beginner Contest 308 A~F
    AtCoderBeginnerContest308手速有点慢A-NewScheme判断给定数字是否满足条件voidsolve(){ boolok=true; inta[10]; for(inti=1;i<=8;i++) cin>>a[i]; for(inti=1;i<=8;i++) { if(i>=2&&a[i]<a[i-1]) ok=......
  • Shortest Time(最短时间)
    题目描述如图所示,有若干个城市,它们之间有道路连通,可以互相到达,从一个城市到另一个城市时间为1。现在给出起点城市A,终点城市B,和N条道路。问从A到B最短时间。Input第一行A,B,N(A,B,N<=30),B为最大城市标号;接下来N行,每行两个数x,y,表示城市x和城市y有道路相连。Output最短到达时间......
  • 将MembershipCreateStatus枚举成员翻译成自定义信息
    publicstaticclassAccountValidation{publicstaticstringErrorCodeToString(MembershipCreateStatuscreateStatus){switch(createStatus){caseMembershipCreateStatus.DuplicateUserName:......
  • AtCoder Beginner Contest 308
    A:1#include<cstdio>2#include<cstring>3#include<algorithm>4#include<iostream>5#include<string>6#include<vector>7#include<stack>8#include<bitset>9#include<cstdlib>10#include......
  • AtCoder Grand Contest 021 E Ball Eat Chameleons
    洛谷传送门AtCoder传送门容易发现一个变色龙是红色当且仅当,设\(R\)为红球数量,\(B\)为蓝球数量,那么\(R\geB\)或\(R=B\)且最后一个球是蓝球。考虑如何判定一个颜色序列是否可行。考虑贪心。若\(R<B\)显然不行。若\(R\geB+n\),每个变色龙都可以分到比蓝球......
  • AtCoder Beginner Contest 307(E,F,G)
    AtCoderBeginnerContest307(E,F,G)E(dp)E这个题大意就是我们需要组成一个长度为\(n\)的数组,满足两个相邻的数字不可以相等,其中,\(a_1\)和\(a_n\)也是相邻的,我们可以选择的数字为\(0\)到\(m-1\),问最后有多少种不同的组成方式满足以上条件。题目大意很简单,就是有点难想,如果\(a......
  • Educational Codeforces Round 151 [div.2 #A-C] 赛后总结(contest/1845)
    link\(\textcolor{lightgreen}{A}-\textcolor{yellow}{B}-\textcolor{yellow}{C}-\textcolor{red}{D}-\textcolor{red}{E}-\color{red}{F}\)A给你一个数n,在给你一个数列1~k,其中x不能用,然后用其他的数任意累加,如能得到n,输出所用数字数量和具体数列。一眼分类。先分是......
  • pytest + yaml 框架 -42.支持 mock 功能
    前言前面已经通过代理实现了抓包自动生成yaml用例的功能,通过代理也可以实现mock功能。mock有2种场景:1.直接拦截发出去的请求,还未到达服务端,模拟自定义返回结果2.发出去的请求,服务端有反回,拦截返回的结果,篡改返回内容,模拟自己需要的数据拦截发出去的请求先看第一种场景:直接......