简体   繁体   中英

Reading CSV file into an array in Java [incompatible types: Integer cannot be converted to int[].]

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 an int[] , lines is a List of Integer and so when we try to get any element in lines it will always return an Integer.

So int thirdCountry[] = lines.get(3); basically means int[] = some int value and that's what causes the above issue. So inorder to fix it I declared lines like this ArrayList<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.

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<String[]> lines = new ArrayList<String[]>();
    String line = null;

    try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
        int i = 0;
        while(((line = bufferedReader.readLine()) != null) && i<27) {
            lines.add(line.split(","));
            System.out.println(Arrays.toString(lines.get(i)));
            i++;
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    if(lines.size() > 3) {
        String thirdCountry[] = lines.get(3);
            
        if(thirdCountry.length > 6) {
            String cp3 = thirdCountry[6];
            System.out.println(cp3);
        }
    }

}

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 this

String s1 = "";
int total = 0;
try {
    total += Integer.parseInt(s1);
} catch (NumberFormatException e) {
    System.out.println("Not a number!");
}
System.out.println(total);

You can modify this for long and double as per your comfort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM