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

发布于 2024-11-05 22:28:09 字数 1244 浏览 1 评论 0原文

我有以下代码,其中逐行读取 .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 have the following code, where a .csv file is read, line by line, and stored in String[] nextline , where the nextline[i] is populated by the respective content of the parsed file.
I got no problems in reading the .csv file, but i do have in storing them in another String[], since the System.out.println() gives me the following result, for all the lines:

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

As you can see, the Strin[] has null value, why the content is not passed?
Thanks in advance

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");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2024-11-12 22:28:09

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

Your i++ statement is located before the System.out so you are actually printing the next line (not parsed yet). You should move the i++ line after the System.out

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文