java - 将值 String[] 传递给 String[] 值

标签 java string opencsv

我有以下代码,其中逐行读取 .csv 文件并将其存储在 String[] nextline 中,其中 nextline[i] 由解析文件的相应内容填充。 我在读取 .csv 文件时没有遇到任何问题,但我确实将它们存储在另一个 String[] 中,因为 System.out.println() 为所有行提供了以下结果:

Nextline[1]=Jan  4, 2011 18:33:15.988422000 / predata=null / Nextline[4]=54 / size:0

可以看到,Strin[]的值为空,为什么内容没有传递呢? 提前致谢

public class Amostra {
    int[] id = new int[20000];
    String[] predata = new String[20000];
    int[] size = new int[20000];

    Amostra(String namefile) throws FileNotFoundException, IOException {
        Csv2Array(namefile);
    }

    private void Csv2Array(String newfile) throws FileNotFoundException, IOException 
    {
        String[] nextline;
        int j = 0;
        int i = 0;
        CSVReader reader = new CSVReader(new FileReader(newfile), ';', '\'', 1);
        while((nextline = reader.readNext()) != null){
            predata[i] = nextline[1];
            size[i] = Integer.parseInt(nextline[4]);
            id[i] = i;
            i++;
            System.out.println("Nextline[1]=" + nextline[1] + 
                    " / predata=" + predata[i] + 
                    " / Nextline[4]=" + nextline[4] + 
                    " / size:" + size[i] + "\n");
        }
    }
}

最佳答案

您的i++语句位于System.out之前,因此您实际上正在打印下一行(尚未解析)。您应该将 i++ 行移到 System.out

之后

关于java - 将值 String[] 传递给 String[] 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5909625/

相关文章:

java - 读取 .tsv 文件时跳过备用行

java - ArrayList 中的对象是否仍然有引用?

java - 如何以编程方式从keycloak(Spring)通过不记名 token 获取用户名和uuid?

java - 如果构造函数以异常结束,创建的对象是否与普通对象相同?

PHP 从传递的变量构建 MySQL 查询字符串

java - 字符串连接: concat() vs "+" operator

java - 如何创建动态对象列表

java - 如何更改此设置,使它无法实现我想要的方式?

java - 有什么方法可以在 api 级别 25 下执行类似 `requestDismissKeyGuard` 的操作吗?

javascript - 获取其中包含连字符的字符串的最后一个子字符串值