gpt4 book ai didi

java - 在 Java vector 中插入 null

转载 作者:行者123 更新时间:2023-12-04 05:44:29 25 4
gpt4 key购买 nike

我正在尝试使用标记器将 .txt 文件的内容插入到 vector 中;但没有插入特定文件。后来我需要将 vector 的内容稍后传递给 jtable。问题是 .txt 的第一行有空值,所以它没有被插入它跳过第一行并继续前进..有没有办法将 null 存储在 vector 中并将其替换为“”?

private void loadSecondPreview() {
String aLine;
recordData = new Vector();
columnNames = new Vector();

try {
FileInputStream fin = new FileInputStream(dataFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
// extract column names
if (delimiter == ",") {
StringTokenizer st1 =
new StringTokenizer(br.readLine(), ",");
while (st1.hasMoreTokens()) {
columnNames.addElement(st1.nextToken());
}
// extract data
while ((aLine = br.readLine()) != null) {
StringTokenizer st2 =
new StringTokenizer(aLine, ",");
while (st2.hasMoreTokens()) {
recordData.addElement(st2.nextToken());
}
}
} else if (delimiter == "\t") {
StringTokenizer st1 =
new StringTokenizer(br.readLine(), "\t");
while (st1.hasMoreTokens()) {
if (st1.equals(null)) {
columnNames.addElement(" ");
} else {
columnNames.addElement(st1.nextToken());
}
}
// extract data
while ((aLine = br.readLine()) != null) {
StringTokenizer st2 =
new StringTokenizer(aLine, "\t");
while (st2.hasMoreTokens()) {
if (st2.equals(null)) {
recordData.addElement(" ");
} else {
recordData.addElement(st2.nextToken());
}
}
}
}
br.close();

System.out.println("Column vector size:" + columnNames.size());
System.out.println("Record vector size:" + recordData.size());

for (int x = 0; x < columnNames.size(); x++) {
System.out.println("Column vector:" + x + " " + columnNames.elementAt(x));
}

for (int x = 0; x < recordData.size(); x++) {
System.out.println("Record vector:" + x + " " + recordData.elementAt(x));
}

开/关:
**列 vector 大小:0
记录 vector 大小:5
记录 vector :0 8
记录 vector :1 8.100
记录 vector :2 8.200
记录 vector :3 9
记录 vector :4 9.8

测试文件记录:第一行有两个空值列

8 8.100
8.200
9 9.8

最佳答案

尝试这个

BufferedReader in = new BufferedReader(new FileReader(loc + fname));
ArrayList<String []> input = new ArrayList<String []>();
String [] temp;
String line;
String stemp = null, sprev;
ArrayList<String> comp = new ArrayList<String>();
while((line = in.readLine())!= null)
{
StringTokenizer st = new StringTokenizer(line, ",", true);
temp = new String [15];
int i = 0;
while(st.hasMoreTokens())
{
sprev = stemp;
stemp = st.nextToken();
if((stemp.equals(sprev)) && stemp.equals(","))
{
//it is a null field
temp[i++] = null;
}
else if(!(stemp.equals(",")))
{
temp[i++] = stemp;
}
}
input.add(temp);

为我工作

关于java - 在 Java vector 中插入 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10860121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com