Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert CSV values to a HashMap key value pairs in JAVA

Tags:

java

hashmap

csv

HI I have a csv called test.csv . I am trying to read the csv line by line and convert the values into a hash key value pairs . Here is the code :-

public class Example {
public static void main(String[] args) throws ParseException, IOException {
    // TODO Auto-generated method stub

    BufferedReader br = new BufferedReader(new FileReader("test.csv"));
    String line =  null;
    HashMap<String,String> map = new HashMap<String, String>();

    while((line=br.readLine())!=null){
        String str[] = line.split(",");
        for(int i=0;i<str.length;i++){
            String arr[] = str[i].split(":");
            map.put(arr[0], arr[1]);
        }
    }
    System.out.println(map);
 }
}

The csv file is as follows :-

1,"testCaseName":"ACLTest","group":"All_Int","projectType":"GEN","vtName":"NEW_VT","status":"ACTIVE","canOrder":"Yes","expectedResult":"duplicateacltrue"
2,"testCaseName":"DCLAddTest","group":"India_Int","projectType":"GEN_NEW","vtName":"OLD_VT","status":"ACTIVE","canOrder":"Yes","expectedResult":"invalidfeaturesacltrue"

When I run this code I get this error :-

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    Example.main(Example.java:33)

Can anyone please help me to fix the code and find out the error in my program ?

like image 928
Newbie Avatar asked Nov 19 '13 09:11

Newbie


2 Answers

In your String when you split it on first time only contains arr[0] as 1 nothing in arr[1] so it will cause an Exception

If you does not need the 1,2, etc.. You can look the following code:

        String str[] = line.split(",");
        for(int i=1;i<str.length;i++){
            String arr[] = str[i].split(":");
            map.put(arr[0], arr[1]);
        }
like image 83
Rakesh KR Avatar answered Sep 23 '22 04:09

Rakesh KR


The problem is that when you split your str, the first element in each line is alone (i.e 1 and 2). So arr only contains ["1"], and hence arr[1] doesn't exists.

I.e for the example input :

1,"testCaseName":"ACLTest"

split by , => str contains {1, testCaseName:ACLTest}
split by : at the first iteration => arr contains {1}

Example :

String s = "1,testCaseName:ACLTest";
String str[] = s.split(",");
System.out.println(Arrays.toString(str));
for(String p : str){
    String arr[] = p.split(":");
    System.out.println(Arrays.toString(arr));
}

Output :

[1, testCaseName:ACLTest]
[1] //<- here arr[1] doesn't exists, you only have arr[0] and hence the ArrayIndexOutOfBoundsException when trying to access arr[1]
[testCaseName, ACLTest]


To fix your code (if you don't want to use a CSV parser), make your loop starting at 1 :
for(int i=1;i<str.length;i++){
      String arr[] = str[i].split(":");
      map.put(arr[0], arr[1]);
}


Another problem is that the HashMap use the hashCode of the keys to store the (key, value) pairs.

So when insering "testCaseName":"ACLTest" and "testCaseName":"DCLAddTest", the first value will be erased and replace by the second one :

Map<String, String> map = new HashMap<>();
map.put("testCaseName","ACLTest");
map.put("testCaseName","DCLAddTest");
System.out.println(map);

Output :

{testCaseName=DCLAddTest}

So you have to fix that too.

like image 23
Alexis C. Avatar answered Sep 22 '22 04:09

Alexis C.