我目前正在尝试编写一个扫描程序,它使用数据集并将其解析为数组。但是我遇到了例外:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at neural.neuralmain.readFile(neuralmain.java:34)
at neural.neuralmain.<init>(neuralmain.java:97)
at neural.neuralmain.main(neuralmain.java:201)
我在这里看到了很多关于这个主题的主题,而且大多数似乎与检查实际上是否有可用于扫描仪的下一个项目有关。我的印象是我的代码做了这个,所以一些帮助将不胜感激。
以下是相关的代码部分:
public int[][] readFile( int[][] inputArray, String filename, int dataType) {
// A "try" block is used, as the constructor will throw an Exception should the filename not be recognised.
try {
// The code reads from the scanner, in, which is attached to the filename.
Scanner input = new Scanner( new File( filename ) );
//Initialising the array to store the input data.
inputArray = new int[dataType][65];
// The data files are arranged into characters, with the first 64 values corresponding to one 4x4 area and
// the 65th corresponds to the character class. This combination loops writes the input file as an array.
// The class is the zeroth element and 1st-64th elements are the divided pixel areas.
for ( int i = 0; i <= dataType; i++ ){
for ( int j = 1; j <= 65; j++ ){
while( !input.hasNextInt() ){
input.next();
}
inputArray[i][j] = input.nextInt();
}
while( !input.hasNextInt() ){
input.next();
}
inputArray[i][0] = input.nextInt();
}
// When the program exits from the two previous "for" loops the program has finished reading from
// the scanner, "input", and the scanner is closed.
input.close();
System.out.println("File successfully read in.");
return inputArray;
}
catch (IOException error){
// If the "try" block fails due to a file not being found, then an exception will be thrown.
System.out.println("File name not recognised. A blank array has been produced.");
// An array of zeroes is created
inputArray = new int[dataType][65];
return inputArray;
}
}
谢谢!
(如果它有助于我使用Eclipse Luna Service Release 2(4.4.2))