I have a CSV file that looks like:

and I have read this into an ArrayList and would like to index through the ArrayList and print the value (these values vary in types, eg. String, double and int). The code I have tried is:
public static void main(String[] args) throws IOException
{
System.out.println("Data from CSV file to be analysed:"+"\n");
String file = "jrc-covid-19-all-days-of-world_ASSIGNMENT-FIXED.csv";
ArrayList<Integer> lines = new ArrayList<Integer>();
String line = null;
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file)))
{
int i = 0;
while(((line = bufferedReader.readLine()) != null) && i<27)
{
String[] values = line.split(",");
i++;
System.out.println(Arrays.toString(values));
}
}
catch (IOException e)
{
e.printStackTrace();
}
int thirdCountry[] = lines.get(3);
int cp3 = thirdCountry[6];
System.out.println(cp3); //should return the value 107122
}
But I got the error message: incompatible types: Integer cannot be converted to int[].
1. Dealing with this error: Integer cannot be converted to int[]
We can only assign
int[]to anint[],linesis a List of Integer and so when we try to get any element inlinesit will always return an Integer.So
int thirdCountry[] = lines.get(3);basically meansint[] = some int valueand that's what causes the above issue. So inorder to fix it I declaredlineslike thisArrayList<String[]> lines.2. Now, why the List is of
String[]type?Since the data can be a String, int, or a double, it is safe to have a
String[]which would accept all three.3. Blind rule while getting any element from an Array or Collection
Whenever you are trying to get some element from an array or collection, always check the length or size and that index should be less than the length of the same.
4. Adding numbers
For adding we need to convert the String values to numeric values (int, long, or double). Let's say we are converting to
int, so the sample values can be "123", "abc", "abc123", or "" (an empty string). So you can try like thisYou can modify this for long and double as per your comfort.