相关疑难解决方法(0)

如何检查String中的String是否为数字

在解析之前,如何检查字符串是否为数字?

java string numeric

838
推荐指数
19
解决办法
136万
查看次数

可以尝试/捕获一些东西只是为了检查是否抛出异常?

尝试使用无用的东西只是为了查看此代码是否抛出特定异常是一种好方法吗?
我想在抛出异常时做一些事情,否则什么也没做.

try {  
    new BigDecimal("some string"); // This do nothing because the instance is ignored  
} catch (NumberFormatException e) {  
    return false; // OK, the string wasn't a well-formed decimal  
}  
return true;
Run Code Online (Sandbox Code Playgroud)

要测试的前提条件太多,构造函数BigDecimal()总是检查它们,所以这似乎是最简单的方法.

java exception

51
推荐指数
4
解决办法
2878
查看次数

Java:异常作为控制流?

我听说使用控制流异常是不好的做法.你觉得这怎么样?

public static findStringMatch(g0, g1) {

    int g0Left = -1;
    int g0Right = -1;
    int g1Left = -1;
    int g1Right = -1;

//if a match is found, set the above ints to the proper indices
//...
//if not, the ints remain -1

        try {
            String gL0 = g0.substring(0, g0Left);
            String gL1 = g1.substring(0, g1Left);

            String g0match = g0.substring(g0Left, g0Right);
            String g1match = g1.substring(g1Left, g1Right);

            String gR0 = g0.substring(g0Right);
            String gR1 = g1.substring(g1Right);

            return new StringMatch(gL0, gR0, g0match, g1match, gL1, gR1); …
Run Code Online (Sandbox Code Playgroud)

java exception control-flow

10
推荐指数
4
解决办法
9846
查看次数

标签 统计

java ×3

exception ×2

control-flow ×1

numeric ×1

string ×1