'How to remove any special character from .csv files using java

im trying to get minmax values from a csv, but some of the values on the csv is giving me a NumberExpectedException here is the code im using



 private static void minMaxValue(String path) {
        try (Stream<String> stream = Files.lines(Paths.get(path)).skip(1)) {
            DoubleSummaryStatistics statistics = stream
                    .map(s -> s.split(",")[3])
                    .mapToDouble(Double::valueOf)
                    .summaryStatistics();
            System.out.println("Lowest:: " + statistics.getMin());
            System.out.println("Highest:: " + statistics.getMax());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


and the time im running the method i sorrounded it with a try/catch but it stills is not the solution, i want to replace or ignore all special characters, this is the line that give me error:

Brittany Smith,Programme researcher broadcasting/film/video,Sawyer-Nelson,2901,0.98,8913 Mckay Loop Johnfurt CO 90828

                                                               ^


Solution 1:[1]

To replace all special characters with emptyString you may achieve easy using:

str = str.replaceAll("[^a-zA-Z0-9]", " "); 

this way, you will have only letters and digits

Solution 2:[2]

Since you're asking for a regex that gets rid of special characters, I guess only to leave characters representing a number (-, ., \d), then this is the regex you're looking for:

String s = s.replaceAll("[^\\-\\.\\d]", ""));

Solution 3:[3]

Posting on the basis that your actual problem is caused by the wrong number of separators. That of course is speculation since we haven't seen your actual input. The following would avoid an error of that sort but won't help with diagnosis (streams are unhelpful with errors)

private static void minMaxValue(String path) {
    try (Stream<String> stream = Files.lines(Paths.get(path)).skip(1)) {
        DoubleSummaryStatistics statistics = stream
            .map(s -> s.split(","))
            .filter(s -> s.length == 6) // Don't use magic numbers like this ;)
            .map(s -> s[3])
            .mapToDouble(Double::valueOf)
            .summaryStatistics();
        System.out.println("Lowest:: " + statistics.getMin());
        System.out.println("Highest:: " + statistics.getMax());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ionut Adrian Ene
Solution 2 Dan
Solution 3 g00se