0

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?

4
  • 3
    Looks like the file potentially contains a section with double quotes. Is the file content you provided the actual file content this output is from? Commented Apr 5, 2020 at 17:46
  • @Jason Yes, it is the actual file. I checked whether there are double quotes but I couldn't find. Commented Apr 5, 2020 at 17:48
  • Have you examined splitData while debugging? Commented Apr 5, 2020 at 17:52
  • Debugger time. Look at the value when the exception is thrown. Commented Apr 5, 2020 at 17:57

3 Answers 3

2

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 " e.g. ""1.0"" which should be stored as "1.0".

Sign up to request clarification or add additional context in comments.

4 Comments

Proper solution, +1.
Why does it split at all? Pattern will never match.
@PM77-1 - I didn't understand what you mean. The output of String pathCSV = "1.0 8.0 4.0 6.0"; String[] splitData = pathCSV.split("\\s+"); System.out.println(Arrays.toString(splitData)); will be [1.0, 8.0, 4.0, 6.0].
@ArvindKumarAvinash - Sorry I was not clear. I have no problems with your solution. I was referring to your analysis of the original problem. I still do not understand where extra quotes come from.
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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.