I have a CSV file that looks like: enter image description here

我已将其读入ArrayList,并希望通过ArrayList进行索引并打印值(这些值的类型不同,例如字符串、double和int).我try 过的代码是:

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

但我收到了错误消息:不兼容的类型:整数不能转换为int[].

推荐答案

1. Dealing with this error: Integer cannot be converted to int[]

我们只能将int[]赋给int[]lines是一个整数列表,所以当我们try 获取lines中的任何元素时,它总是返回一个整数.

所以int thirdCountry[] = lines.get(3);基本上意味着int[] = some int value,这就是造成上述问题的原因.为了修复它,我声明了lines,比如ArrayList<String[]> lines.

2. Now, why the List is of 100 type?

由于数据可以是字符串、int或double,因此可以使用String[]来接受这三个值.

3. Blind rule while getting any element from an Array or Collection

每当您试图从数组或集合中获取某些元素时,请始终判断长度或大小,并且索引应小于相同的长度.

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

对于添加,我们需要将字符串值转换为数值(int、long或double).假设我们将转换为int,因此样本值可以是"123"、"abc"、"abc123"或""(空字符串).所以你可以这样试试

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

您可以根据自己的舒适度对其进行长时间修改和翻倍.

Java相关问答推荐

JLS中形式参数列表后面的任何括号对用于确定方法结果中的精确数组类型的具体含义是什么?

为什么JFrame paint()多次绘制同一点(或根本不绘制)?

ActivityCompat.请求收件箱自动拒绝权限

Mat. n_Delete()和Mat. n_release的区别

Java WireMock定义存根在Cucumber并行执行的多线程测试中失败

为什么我们仍然需要实现noArgsConstructor如果Java默认提供一个非参数化的构造函数?''

Java模式匹配记录

无法传递消费者<;>;实例

存根基类的受保护方法

在Spring Boot应用程序中导致";MediaTypeNotSupportdException&qot;的映像上载

Java构造函数分支

Java中不兼容的泛型类型

当b是一个字节并且在Java中值为-1时,为什么b>;>;>;1总是等于-1?

如何使用Criteria Builder处理一对多关系中的空值?

如何在不作为类出现的表上执行原生查询?

如何从命令行编译包中的所有类?

循环不起作用只有第一个元素重复

如何在Spring Boot中为不同的部署环境管理多个.properties文件?

当我将JTextField的getText函数与相等的String进行比较时;t返回true

在数组中查找素数时出现逻辑错误