将值 String[] 传递给 String[] 值
我有以下代码,其中逐行读取 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 i++ 语句位于 System.out 之前,因此您实际上正在打印下一行(尚未解析)。您应该将
i++
行移到System.out
之后Your
i++
statement is located before theSystem.out
so you are actually printing the next line (not parsed yet). You should move thei++
line after theSystem.out