NoSuchElement 异常似乎无缘无故被调用

NoSuchElement Exception seems to be called for no reason

我正在尝试编写一个程序来读取数据文件,有点像这样:

01 1 2 3 4 5
02 1 2 3 4 5
03 1 2 3 4 5

等等,到目前为止我有这个代码:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class fileReader {
    int lineSpaceCount; // number of spaces per line in the input
    int outputSpaceCount; // number of spaces the output will use
    int[] includeData; // data value places that will be included into the output
    String returnText; // text that will be returned

    // CONSTRUCTORS
    public fileReader() {
        this.lineSpaceCount = 0;
        this.outputSpaceCount = 0;

        this.includeData = new int[outputSpaceCount];
    } // default constructor that sets everything to default values

    public fileReader(final int lineSpaceCount, final int outputSpaceCount) {
        this.lineSpaceCount = lineSpaceCount;
        this.outputSpaceCount = outputSpaceCount;

        this.includeData = new int[outputSpaceCount];
    } // constructor to set everything to custom values

    // SETTER METHODS
    public void setIncludeData(final int[] inputArray) {
        for (int i = 0; i < this.includeData.length; i++) {
            this.includeData[i] = inputArray[i];
        }
    }

    public void setOutputSpaceCount(final int outputSpaceCount) {
        this.outputSpaceCount = outputSpaceCount;
    }

    public void setLineSpaceCount(final int lineSpaceCount) {
        this.lineSpaceCount = lineSpaceCount;
    }

    // GETTER METHODS
    public int getLineSpaceCount() {
        return this.lineSpaceCount;
    }

    public int getOutputSpaceCount() {
        return this.outputSpaceCount;
    }

    public int[] getIncludeData() {
        return this.includeData;
    }

    // CUSTOM METHODS
    public String splitFile(final File file) throws IOException {
        int fileLength = 0;
        
        try (
            Scanner counter = new Scanner(new FileReader(file))
            ) {
            while (counter.hasNextLine()) {
                counter.nextLine();
                fileLength++;
                } //counts number of lines in the file
            } //automatically closes the scanner

        final String[] inArray = new String[fileLength]; // array that will contain every line in the file
        String[] secArray = new String[this.lineSpaceCount]; // array that will contain every part of the line for inArray

        try (
            final Scanner input = new Scanner(file);
            ) {
            for (int i = 0; i > -1; i++) {
                inArray[i] = input.nextLine(); //this is where the NoSuchElementException happens

                if (i > fileLength) {
                    i = -1;
                } //if there are no more lines, end loop
            } //puts all the lines of the file into a massive array
        } //automatically closes the scanner

        final String[][] outArray = new String[inArray.length][this.outputSpaceCount]; // output array that will betranslated into text, has to be placed here for the .length attribute of inArray to be correct

        for (int h = 0; h < fileLength; h++) {
            secArray = inArray[h].split(" "); //splits the line into portions

            for (int k = 0; k < secArray.length; k++) {
                outArray[h][k] = secArray[k]; // h = rows, k = columns
            }
        } // nested loops to transfer information into the 2D array

        toText(outArray, fileLength);
        return this.returnText;
    } // massive method that splits a file into individual parts

    public void toText(final String[][] array, final int arrayLength) {
        for (int i = 0; i < arrayLength; i++) {
            for (int j = 0; j < includeData.length; j++ ) {
                this.returnText += array[i] [includeData[j]] + " ";
                } 
            this.returnText += "\n";
            } //method turns the 2D array into a neat output
            //if includeData = {0, 2} and outArray = {{0, 2, 3}{8, 9, 14}} then returnText will equal to 0 3 \n 8 14
        } //method that converts the 2D array into readable text
    }

我写了一个简单的 main 方法来尝试使用文件 PovertyData.txt,打印到 output.txt。

import java.io.File;
import java.io.PrintWriter;

public class Main {
    public static void main(String []args) throws Exception {
        try (
            PrintWriter output = new PrintWriter("outputData.txt");
            ) {
            File povertyFile = new File("PovertyData.txt"); //importing the file
            int [] includeData = {1, 3, 4, 5}; //all the data values needed for the output
        
            fileReader readFiles = new fileReader(7, 4);
            readFiles.setIncludeData(includeData);
            
            output.println(readFiles.splitFile(povertyFile));
            }
        catch (java.io.IOException ex) {
            System.out.println("Error: File does not exist");
            }
        }
    }

这是 PovertyData.txt 的摘录(超过 10,000 行)

01 00190 Alabaster City School District                                              31754     6475      733 USSD13.txt 24NOV2014  
01 00005 Albertville City School District                                            21522     4010     1563 USSD13.txt 24NOV2014  
01 00030 Alexander City City School District                                         17292     2719     1158 USSD13.txt 24NOV2014  
01 00060 Andalusia City School District                                               9044     1512      471 USSD13.txt 24NOV2014  
01 00090 Anniston City School District                                               22759     3304     1122 USSD13.txt 24NOV2014  
01 00100 Arab City School District                                                    8238     1491      250 USSD13.txt 24NOV2014  
01 00120 Athens City School District                                                 23494     3767      770 USSD13.txt 24NOV2014  
01 00180 Attalla City School District                                                 6019      971      283 USSD13.txt 24NOV2014  
01 00210 Auburn City School District                                                 57441     7120      994 USSD13.txt 24NOV2014  
01 00240 Autauga County School District                                              55246    10643     1855 USSD13.txt 24NOV2014  
01 00270 Baldwin County School District                                             195540    32652     6171 USSD13.txt 24NOV2014  
01 00300 Barbour County School District                                              14124     1769      759 USSD13.txt 24NOV2014  
01 00330 Bessemer City School District                                               27510     4765     2009 USSD13.txt 24NOV2014  
01 00360 Bibb County School District                                                 22512     3590     1096 USSD13.txt 24NOV2014  
01 00390 Birmingham City School District                                            212631    30718    12740 USSD13.txt 24NOV2014  
01 00420 Blount County School District                                               51246     9201     2218 USSD13.txt 24NOV2014  
01 00012 Boaz City School District                                                    9619     1742      464 USSD13.txt 24NOV2014  
01 00450 Brewton City School District                                                 5354      866      279 USSD13.txt 24NOV2014  
01 00480 Bullock County School District                                              10639     1565      684 USSD13.txt 24NOV2014  
01 00510 Butler County School District                                               20265     3494     1430 USSD13.txt 24NOV2014  
01 00540 Calhoun County School District                                              56502     9945     2162 USSD13.txt 24NOV2014  
01 00600 Chambers County School District                                             27696     4343     1348 USSD13.txt 24NOV2014  
01 00630 Cherokee County School District                                             26203     4163     1184 USSD13.txt 24NOV2014  
01 00188 Chickasaw City School District                                               6120     1028      443 USSD13.txt 24NOV2014  
01 00660 Chilton County School District                                              43951     7767     2503 USSD13.txt 24NOV2014  
01 00690 Choctaw County School District                                              13426     2160      616 USSD13.txt 24NOV2014  
01 00720 Clarke County School District                                               19459     3399     1146 USSD13.txt 24NOV2014  
01 00750 Clay County School District                                                 13486     2195      604 USSD13.txt 24NOV2014  
01 00780 Cleburne County School District                                             14631     2505      637 USSD13.txt 24NOV2014  
01 00810 Coffee County School District                                               18996     3302      719 USSD13.txt 24NOV2014  
01 00840 Colbert County School District                                              23763     3740      947 USSD13.txt 24NOV2014  
01 00870 Conecuh County School District                                              12887     2122      843 USSD13.txt 24NOV2014  
01 00900 Coosa County School District                                                10898     1557      439 USSD13.txt 24NOV2014  
01 00930 Covington County School District                                            22163     3506      745 USSD13.txt 24NOV2014  
01 00960 Crenshaw County School District                                             13986     2350      693 USSD13.txt 24NOV2014  
01 00990 Cullman City School District                                                14740     2313      555 USSD13.txt 24NOV2014  
01 01020 Cullman County School District                                              66044    11111     2083 USSD13.txt 24NOV2014  
01 01050 Dale County School District                                                 15140     2540      635 USSD13.txt 24NOV2014  
01 01080 Daleville City School District                                              11422     1883      573 USSD13.txt 24NOV2014  
01 01110 Dallas County School District                                               22104     4098     1860 USSD13.txt 24NOV2014  
01 01140 DeKalb County School District                                               56920    10543     2800 USSD13.txt 24NOV2014  
01 01170 Decatur City School District                                                55850     9364     2657 USSD13.txt 24NOV2014  
01 01200 Demopolis City School District                                               7172     1259      766 USSD13.txt 24NOV2014  
01 01230 Dothan City School District                                                 67258    11608     3349 USSD13.txt 24NOV2014  
01 01260 Elba City School District                                                    5282      830      322 USSD13.txt 24NOV2014  
01 01290 Elmore County School District                                               73612    12654     2392 USSD13.txt 24NOV2014  
01 01320 Enterprise City School District                                             27130     4813     1228 USSD13.txt 24NOV2014  
01 01350 Escambia County School District                                             32629     5146     1786 USSD13.txt 24NOV2014  
01 01380 Etowah County School District                                               60104    10265     1937 USSD13.txt 24NOV2014  
01 01410 Eufaula City School District                                                12952     2368      924 USSD13.txt 24NOV2014  

但是,在第 75 行,inArray[i] = input.nextLine();,有一个 NoSuchElement 异常,错误消息为:

Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
        at fileReader.splitFile(fileReader.java:75)
        at Main.main(Main.java:15)

PovertyData.txt 的第 1651 行如下:

08 03630 Edison School District 54-JT                                                  336       79       14 USSD13.txt 24NOV2014  

我不知道为什么会这样,因为“文件”中有数据,而该语句所做的只是将文件的每一行读取到数组中。请告诉我为什么会发生这种情况或如何解决它。

谢谢。

您在读取文件时的 for 循环中存在错误。 除了修复它,还有一种更简单的方法可以从 Java.

中的文件中读取所有行
List<String> allLines = Files.readAllLines(file.toPath());
String[] inArray = new String[allLines.size()];
allLines.toArray(inArray);