Java 似乎无缘无故地调用NoTouchElement异常
我正在尝试制作一个程序来读取数据文件,有点像这样:Java 似乎无缘无故地调用NoTouchElement异常,java,exception,nosuchelementexception,Java,Exception,Nosuchelementexception,我正在尝试制作一个程序来读取数据文件,有点像这样: 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 s
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
}
这里是PovertyData.txt的摘录(超过10000行)
但是,在第75行,inArray[i]=input.nextLine()代码>,出现NoTouchElement异常并显示此错误消息:
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
我不知道为什么会发生这种情况,因为“文件”中有数据,Station所做的就是将文件的每一行读取到一个数组中。请告诉我为什么会发生这种情况或如何解决它
谢谢。在读取文件时,for循环中有一个bug。
在Java中,有一种更简单的方法来读取文件中的所有行,而不是修复它
List<String> allLines = Files.readAllLines(file.toPath());
String[] inArray = new String[allLines.size()];
allLines.toArray(inArray);
List allLines=Files.readAllLines(file.toPath());
String[]inArray=新字符串[allLines.size()];
所有行。toArray(inArray);
for(int i=0;i>-1;i++)对这种循环条件有什么期望{
?尝试i
而不是i>-1
。我看到的一个问题是,您对同一数据使用多个扫描仪。问题是,如果关闭一个扫描仪,文件也会关闭。@nomaker我该如何解决这个问题?只需对每个文件使用一个扫描仪,并且在程序结束之前不要关闭它。@hev1谢谢,th在suggestion+magikarp的回答中修复了它:)您找到错误了吗?是的,因为循环条件无效,而不是i>-1
,它应该是i
,就像@hev1注释的那样。
08 03630 Edison School District 54-JT 336 79 14 USSD13.txt 24NOV2014
List<String> allLines = Files.readAllLines(file.toPath());
String[] inArray = new String[allLines.size()];
allLines.toArray(inArray);