gpt4 book ai didi

java - br.readLine() = ","时生成 ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-29 06:29:53 31 4
gpt4 key购买 nike

下面的代码有几个函数,这些函数允许诸如将数据写入文档、读取文档以及将数据放入数组中以用于稍后的 JTable 等操作。

package tabletest.populatetable;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class GetData {
DataClass[] data = new DataClass[500];
int nextPosition = 0;

public GetData() {
readData();
}

public void writeData()
{
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("resources/tabledata.txt")));

for(int i=0; i < nextPosition; i++) {
bw.write(data[i].toString());
bw.newLine();
}

bw.close();
} catch(Exception e) {
System.out.println("Invalid Data input");
}
}

public void readData()
{
try {
BufferedReader br = new BufferedReader(new FileReader(new File("resources/tabledata.txt")));
String nextData = br.readLine();
String[] arrayStringData;

while (nextData != null) {

try {
arrayStringData = nextData.split(",");
} catch(NullPointerException nPE) {
arrayStringData = new String[] {" ", " "};
}

for(int i = 0; i < arrayStringData.length - 1; i++) {
if(arrayStringData[i] == null || arrayStringData[i] == "") {
arrayStringData[i] = " ";
}
}

DataClass getData = new DataClass();
getData.col1 = arrayStringData[0].trim();
getData.col2 = arrayStringData[1].trim();
data[nextPosition] = getData;
nextPosition++;
nextData = br.readLine();
}

br.close();
} catch(Exception e) {
e.printStackTrace();
}
}

public String[][] dataInTableForm() {
final int colCount = 2;
String[][] temp = new String[nextPosition][colCount];

for(int i = 0; i < nextPosition; i++) {
temp[i][0] = data[i].col1;
temp[i][1] = data[i].col2;
}

return temp;
}

private class DataClass {
String col1;
String col2;

public String toString() {
return col1 + ", " + col2;
}
}
}

它正在读取的文档 resources/tabledata.txt 有 12 行,看起来像这样。

asfias, adsnj
aw,aerfae
aw,aewaa
,tre
asfd,
okfas,af
e,ds
sw,f
,
asfias, adsnj
aw,aerfae
aw,aewaa

问题出在文本文档的第 9 行。这是它本身只是一个 , 的地方。当逗号之前或之后有东西时,这似乎工作正常,我通过删除该行来检查它是否确实是导致问题的逗号。

当我查看控制台时,我发现问题是 ArrayIndexOutOfBoundsException 并且堆栈跟踪如下。

java.lang.ArrayIndexOutOfBoundsException: 0
at tabletest.populatetable.GetData.readData(GetData.java:55)
at tabletest.populatetable.GetData.<init>(GetData.java:14)
at tabletest.Table.createTablePanel(Table.java:76)
at tabletest.Table.createPanels(Table.java:34)
at tabletest.Table.runGUI(Table.java:24)
at tabletest.Table.main(Table.java:150)

代码的第 55 行是 getData.col1 = arrayStringData[0].trim();

正如您在代码中看到的那样,我尝试了几种方法来防止这种情况发生,但我没有成功。我还尝试从行尾删除 .trim() ;但是,出现了完全相同的问题。

如能帮助解决此问题,我将不胜感激。

最佳答案

split(String regex) 的 Javadoc说:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

因此,",".split(",") 将返回一个空数组,即 new String[0]

如果要保留尾随空字符串,请使用 ",".split(",", -1),它将返回 new String[] { "", ""

关于java - br.readLine() = ","时生成 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38275038/

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