简体   繁体   中英

An error when parsing a string to double in Java

I'm trying to read a csv file and convert its String values to double.

public void trq() throws IOException, ParseException {
    Test4 obj = new Test4();
    String path = "transposedData1.csv";
    BufferedReader readerBuffer = new BufferedReader(new FileReader(path));
    String line;
    List<Double> results = new ArrayList<Double>();
    while ((line = readerBuffer.readLine()) != null) {
        if(!line.contains("NA")) {
            ArrayList<String> data_per_class = convertCSVtoArrayList11(line);

            List<Double> doubleList = data_per_class.stream()
                    .map(Double::parseDouble)
                    .collect(Collectors.toList()); // the error here

            // do other things
        }
    }
}

public static ArrayList<String> convertCSVtoArrayList11(String pathCSV) {
    ArrayList<String> result = new ArrayList<String>();

    if (pathCSV != null) {
        String[] splitData = pathCSV.split("\\s*,\\s*");
        for (int i = 0; i < splitData.length; i++) {
            if (!(splitData[i] == null) || !(splitData[i].length() == 0)) {
                result.add(splitData[i].trim());
            }
        }
    }

    return result;
}

The CSV file looks like:

1.0  8.0  4.0  6.0
3.0  2.0  1.0  5.0
7.0  1.0  8.0  1.0
1.0  2.0  1.0  4.0

Note that there is no header. The file starts with values with no headers. The error I am getting is:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""1.0""
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
    at Test4.trq(Test4.java:1063) \\ I mentioned that in the code above

Do you have any idea how to solve this?

Replace

pathCSV.split("\\s*,\\s*");

with

pathCSV.split("\\s+");

So that the numbers are split on spaces. Currently, they are getting stored into result with extra " eg ""1.0"" which should be stored as "1.0" .

Based on the error message you can see, that the number is actually aurrounded by a pair of quotation marks, which probably causes the call to fail.

A quick fix would be to use asString = substring(1, asString.length() - 2); , with asString being the string that you want to parse to a double.

Use result.add(splitData[i].trim(). replaceAll("/"","") ) before returning

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM