方法仅适用于<5000个args参数

时间:2019-03-22 15:22:03

标签: java parsing double args

我对转换类似“ n.n +,n.n +,n.n +,...,n.n +”的字符串的方法有一个小问题。按双顺序。 如果我的值小于4998,则一切工作都很好。如果我有4998或更多,则会引发以下异常:

Exception in thread "main" java.lang.NumberFormatException: For input string: "1.178293466734042,"
        at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
        at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
        at java.base/java.lang.Double.parseDouble(Double.java:543)
        at java.base/java.lang.Double.valueOf(Double.java:506)

这是我写的方法:

private static void parseDoubleArrayFromInput (String[] input, double[] parsedDoubles){
        for(int i = 0 ; i < input.length - 1; i++){                     //Removes the last character, which is the comma, apart from the last valid string.
            if(i == input.length - 2)                                   //Does not consider the last string of the array since it contains only the dot.
                parsedDoubles[i] = Double.parseDouble(input[i]);        //Then, it fills the array of doubles with the parsed double.
            else
                parsedDoubles[i] = Double.parseDouble(input[i].substring(0,input[i].length()-1));
        }
    }

这是生成文件的类

import java.time.*;

public class PRNG {

    public PRNG(){

    }

    public static String generatePseudoRandomNumbers(double actual, int repetitions) {        //main function, generates pseudo random numbers and
        String output = "";
        double[] values = new double[repetitions];
        for(int i = 0; i < repetitions; i++) {
            double nextNumber = Math.pow(7,5)* actual % (Math.pow(2,31)- 1);    //algorithm to generate next pseudo random number
            actual = nextNumber;
            double singleResult = (2*(actual / (Math.pow(2,31) - 2))); //equation to shrink random numbers in a little range (0..100 in this case)
            output = output + singleResult + ", ";
        }
        output = output.substring(0, output.length()-3) + " .";
        return output;
    }

    public static int generatePseudoRandomNumbersTimes(double actual) {
        double nextNumber = Math.pow(7,5)* actual % (Math.pow(2,31)- 1);
        int singleResult = (int)(21*(nextNumber / (Math.pow(2,31) - 2)));
        return singleResult;
    }
}

这是应该接收输入并按双精度顺序对其进行解析的类

import java.util.Arrays;

public class StupidAlg {

    public static void main (String[] args){
        double[] doubleInput;
        if (args.length ==0) {
            System.out.print("");
        }
        else {
            doubleInput = new double[args.length - 1];
            parseDoubleArrayFromInput(args, doubleInput);
            double target = sum(doubleInput) / 2;                   //find target value
            double value = findWeightedMedian(doubleInput, target);
            System.out.print(value);
        }
    }

    private static void parseDoubleArrayFromInput (String[] input, double[] parsedDoubles){
        // process all of the input
        for(int i = 0 ; i < input.length - 1; i++) {
            // remove from the input things that will break the parsing
            // NOTE: other approaches could be used to ensure there is
            //   only a single ".".
            // NOTE: assumes the input to be US standard, as other approachs
            //        might use a "," for separator
            String clean = input[i].replaceAll("[^0-9.]", "");

            // put the result
            parsedDoubles[i] = Double.parseDouble(clean);
        }
    }

    private static double findWeightedMedian (double[] input, double target){
        Arrays.sort(input);
        double sum = 0;
        double result = 0;
        for(double v : input){
            sum += v;
            if (sum >= target) {
                result = v;
                break;
            }
        }
        return result;
    }

    private static double sum (double[] input){
        double result = 0;
        for(double v : input){
            result = result + v;     //sum all values in array
        }
        return result;
    }
}

我之所以使用Arrays库,是因为我不想实现排序算法,因为这是一个简单的问题,而这就是测试文件。

输入文件是通过这种方式制作的

0.2931308777007562, 0.650659222761783, 1.6295518657467811, 1.8781948535500046, 0.8208889158637159, 0.680002497211101, 0.8019653053972547, 0.6308815354768946, 1.2259618232268485, 0.7403533791567696, 1.1192376940690332, 1.0279154591522324, 0.1751139268047306, 1.139766437131694, 0.05449995217332612, 1.9806957514776808, 1.5534795844494176, 1.3313636838750575, 0.22942446845530018, 1.937039533571377, 1.8234255749950423, 0.31362467102112684, 1.08984339804374, 0.9979823920856997, 1.090055974284239, 0.570751264291583 .

2 个答案:

答案 0 :(得分:1)

非常明显的例外-如果输入String为,则Double.parseDouble()(实际上,所有Integer / Long /等.parseXXX()方法都将抛出)格式不正确。

所以,最好是:

  • 捕获异常,或者知道它可以传播
  • 尝试解析之前检查输入的有效性

当前算法假定错误值的位置。这种方法似乎非常脆弱。我会清理输入,并允许异常传播。

private static void parseDoubleArrayFromInput (String[] input, double[] parsedDoubles){
    // process all of the input
    for(int i = 0 ; i < input.length; i++) {
        // remove from the input things that will break the parsing
        // NOTE: other approaches could be used to ensure there is 
        //   only a single ".".
        // NOTE: assumes the input to be US standard, as other approachs
        //        might use a "," for separator
        String clean = input[i].replaceAll("[^0-9.]", "");

        // put the result
        parsedDoubles[i] = Double.parseDouble(clean);
    }
}   

答案 1 :(得分:1)

通常Array内部使用int,因此应允许Integer.MAX_VALUE,但它还取决于要存储到Array中的数据类型及其大小和可用于堆的内存大小。可能不是您要找的答案,但值得尝试。您可以找到详细信息here