Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA:ArrayIndexOutOFBoundException在读取导入的txt时发生_Java_Arrays_Exception - Fatal编程技术网

JAVA:ArrayIndexOutOFBoundException在读取导入的txt时发生

JAVA:ArrayIndexOutOFBoundException在读取导入的txt时发生,java,arrays,exception,Java,Arrays,Exception,我是一个初学者,我有一个txt文件,用户将导入java,我将读取txt文件行,然后在每行上设置变量基,并将它们添加到当前记录中 public void importTXT() { JFileChooser fc = new JFileChooser(); fc.setAcceptAllFileFilterUsed(false); fc.setMultiSelectionEnabled(false); FileNameExtensionFilter filt

我是一个初学者,我有一个txt文件,用户将导入java,我将读取txt文件行,然后在每行上设置变量基,并将它们添加到当前记录中

    public void importTXT() {
    JFileChooser fc = new JFileChooser();
    fc.setAcceptAllFileFilterUsed(false);
    fc.setMultiSelectionEnabled(false);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "TEXT FILES", "txt", "text");
    fc.setFileFilter(filter);
    int returnVal = fc.showOpenDialog(CollectionFrame.this);
    String[] numstrs = null;
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File importedFile = fc.getSelectedFile();

        try {
            Scanner sc = new Scanner(importedFile);
            while (sc.hasNextLine()) {
                numstrs = sc.nextLine().split("\\s+"); // split by white
                                                        // space
            }
        } catch (IOException e) {

        }
        // add new collection
        Collection newCollection = new Collection(numstrs[0]);
        allRecord.addCollection(newCollection);

        // add art consignment information
        String consignmentName = numstrs[3];
        String description = numstrs[4];
我在最后一行的第二行收到了一个
ArrayIndexOutOfBoundsException

String委托名称=numstrs[3]

文本文件的内容如下所示:

Richman’s Estate Collection
5
ART
Water Lilies
A superb piece in great condition
有人能告诉我怎么了吗?

编辑: 您当前正在读取所有行,并每次替换
numstrs

因此,当您离开循环时,您只得到其中最后一行的值。
我想您希望保存所有行-请参见下文。
结束编辑

您应该使用arraylist

像这样:

ArrayList<String[]> numstrsList = new ArrayList<String[]>();
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File importedFile = fc.getSelectedFile();

        try {
            Scanner sc = new Scanner(importedFile);
            while (sc.hasNextLine()) {
                numstrsList.add(sc.nextLine().split("\\s+")); // split by white
                                                        // space
            }
        } catch (IOException e) {

        }
}
ArrayList numstrsList=new ArrayList();
if(returnVal==JFileChooser.APPROVE_选项){
File importedFile=fc.getSelectedFile();
试一试{
扫描仪sc=新扫描仪(导入文件);
while(sc.hasNextLine()){
numstrsList.add(sc.nextLine().split(\\s+);//按白色分割
//空间
}
}捕获(IOE异常){
}
}
您可以通过以下方法获取arraylist的值:

for(int i=0:i<numstrsList.size();i++){
    String[] oneLineStrArray = numstrsList.get(index)
    //do something 
}

for(int i=0:i正如前面的注释所建议的,该文件显然不包含您期望的形式的信息。
我建议您在扫描仪读取后向其添加som错误处理

if (numstrs.length < 5){
  ///TODO: add some handling here, exception or error dialog
}
if(numstrs.length<5){
///TODO:在此处添加一些处理,异常或错误对话框
}
在这里,首先通过

在尝试访问索引值之前,请检查
numstrs
length。您可以

String consignmentName = null, description = null;
if (numstrs.length >= 4) {
    consignmentName = numstrs[3];
}
if (numstrs.length >= 5) {
    description = numstrs[4];
}

另外,正如其中一条注释所指出的-
numstrs
将始终具有最后一行返回的值。

这意味着数组numstrs的元素少于4个。请尝试使用numstrs.lengthWhat
Collection newCollection=newCollection(numstrs[0])
应该做什么?你有自己的类型叫
Collection
吗?因为
java.util中的
Collection
是一个接口,首先检查
numstrs.length>4
你应该使用一个调试器。你知道,通过将它放在循环之外,你只使用最后一行并尝试使用它。检查最后一行w按空格分割时,将不会有4个元素。