-1

I have a sample csv file as follows (a dummy csv file structure for reference):

Col0, Col1, Col2, Col3, Col4, Col5
str1, str2, str3, str4, str5, 45.545
str1, str2, str3, str4, str5, 45.545
str1, str2, str3, str4, str5, 45.545
str1, str2, str3, str4, str5, 45.545
str1, str2, str3, str4, str5, 45.545

I am trying to read this csv file into a HashMap<String, Double> where the key is generated as (Col1 + "|" + Col2 + "|" + Col3 + "|" + Col4 + "|" + Col0) and value as Col5.

I am following a similar article here (How to convert csv to a map using Java 8 Stream ) but getting error while converting Col5 to double as follows:

java.lang.NumberFormatException: empty String

Following is the code I am currently using:

    private void convertCsvtoMap(final String filePath) {
        try {
            Stream<String> lines = Files.lines(Paths.get(filePath));
            Map<String, Double> resMap = lines.skip(1).map(line -> line.split(",")).collect(
                    Collectors.toMap(line -> (line[1] +  "|" + line[2] +  "|" + line[3] + "|" + line[4] + "|" + line[0]), line -> Double.parseDouble(line[5])));


        } catch (IOException e) {
            e.getLocalizedMessage();
        }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
user3243499
  • 2,953
  • 6
  • 33
  • 75
  • Using *this* sample file I get a *different* error. Post a sample CSV which is closer to exhibiting your problem. – Makoto Dec 13 '18 at 19:41
  • It's impossible to tell because your sample file does not reproduce the problem. Find out which line is giving the exception and you'll probably see what's the problem. – Perdi Estaquel Dec 14 '18 at 01:06

2 Answers2

2

I'm not going to give you the entire solution but here is a way to approach this problem,

Steps:

  • Read File (I'm using Scanner).
  • Split the line using , delimiter.
  • Create key and value using the splitted values.
  • Add it to your map.

Code Snippet:

private void convertCsvtoMap(final String filePath) {
    try (Scanner in = new Scanner(new File(filePath))) {
        String line = null;
        Map<String, Double> resMap = new HashMap<>();
        while (in.hasNextLine()) {
            line = in.readLine();
            String[] fields = line.split(",");
            String key = fields[0].trim() + "|" + fields[1].trim() + ...;
            double value = Double.parseDouble(fields[5].trim());
            resMap.put(key, value);
        }
    } catch (Exception ex) {
        /* Handle Exception */
    }
}

Note: I have not compiled the code and it might have a few Syntax issue.

halfer
  • 19,824
  • 17
  • 99
  • 186
user2004685
  • 9,548
  • 5
  • 37
  • 54
0
public static Map<String, String> csvToMap(File csvFile) throws FileNotFoundException {

    final Scanner scanner = new Scanner(csvFile);
    String[] keys = scanner.nextLine().split(",");

    Map<String, String> resultMap = new HashMap<>();

    while (scanner.hasNextLine()) {
        String[] values = scanner.nextLine().split(",");

        for (int i = 0; i < keys.length; i++) {
            resultMap.put(keys[i], values[i]);
        }
    }

    return resultMap;
}

In the above example, values are stored as strings as well. You can parse the values to the value type of the Map, if necessary.

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68