最小的StringTokenizer是错误的?

时间:2015-10-09 13:41:32

标签: java stringtokenizer

我有代码要继续并获得StringTokenizer分钟,但结果为min = 999999

public static double minStringTokenizer(StringTokenizer stz) {
    double min = 999999;
    while (stz.hasMoreTokens()) {
        String temp1 = stz.nextToken();
        double temp2 = Double.parseDouble(temp1);
        if (min > temp2) {
            min = temp2;
        }
    }
    return min;
}

1 个答案:

答案 0 :(得分:0)

这对你有用吗?

public class Main {

    public static void main(String[] args) throws Exception {

        System.out.println(minStringTokenizer(new StringTokenizer("1,2,99,-3,4,888,-1",",")));
        System.out.println(minStringTokenizer(new StringTokenizer("1199999999999,99999999999",",")));
        System.out.println(minStringTokenizer(new StringTokenizer("5,4,3,2",",")));

        //throws err
        //System.out.println(minStringTokenizer(new StringTokenizer("5xxx,4,3,2",",")));
    }

    public static double minStringTokenizer(StringTokenizer stz) {

        if(stz==null || !stz.hasMoreTokens()) {
            throw new IllegalArgumentException("stz may not be null or empty");
        }

        double min = Double.MAX_VALUE;
        while (stz.hasMoreTokens()) {
            String temp1 = stz.nextToken();
            double temp2 = Double.parseDouble(temp1);
            min = Math.min(min, temp2);
        }
        return min;
    }
}