使用 Scanner.nextInt() 的 Java NoSuchElementException
我正在尝试读取 pgm 文件(512x512 数组),当我读取更大的文件时,我收到错误:java.util.NoSuchElementException 读取元素 (3,97)。
我创建了一个更小的文件来读取(23x23),它读取得很好。有尺寸限制吗?我检查了该文件并确认该值有一个 int: 这似乎是它崩溃的行:
fileArray[row][col] = scan.nextInt();
这是文件:
import java.util.Scanner;
import java.io.*;
public class FileReader {
public static void main(String[] args) throws IOException {
String fileName = "lena.pgma";
int width, height, maxValue;
FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(fileName);
Scanner scan = new Scanner(fileInputStream);
// Discard the magic number
scan.nextLine();
// Discard the comment line
scan.nextLine();
// Read pic width, height and max value
width = scan.nextInt();
System.out.println("Width: " + width);
height = scan.nextInt();
System.out.println("Heigth: " + height);
maxValue = scan.nextInt();
fileInputStream.close();
// Now parse the file as binary data
FileInputStream fin = new FileInputStream(fileName);
DataInputStream dis = new DataInputStream(fin);
// look for 4 lines (i.e.: the header) and discard them
int numnewlines = 4;
while (numnewlines > 0) {
char c;
do {
c = (char)(dis.readUnsignedByte());
} while (c != '\n');
numnewlines--;
}
// read the image data
int[][] fileArray = new int[height][width];
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
fileArray[row][col] = scan.nextInt();
System.out.print("(" + row + " ," + col +"): " + fileArray[row][col]+ " ");
}
System.out.println();
}
dis.close();
}
}
任何建议将不胜感激。
I am trying to read in a pgm file (512x512 array) and when I read in a larger file I get the error: java.util.NoSuchElementException on reading element (3,97).
I have created a much smaller file to read (23x23) and it reads fine. Is there a size limit? I have checked the file and confirmed that there is an int for the value:
This appears to be the line it crashes at:
fileArray[row][col] = scan.nextInt();
Here is the file:
import java.util.Scanner;
import java.io.*;
public class FileReader {
public static void main(String[] args) throws IOException {
String fileName = "lena.pgma";
int width, height, maxValue;
FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(fileName);
Scanner scan = new Scanner(fileInputStream);
// Discard the magic number
scan.nextLine();
// Discard the comment line
scan.nextLine();
// Read pic width, height and max value
width = scan.nextInt();
System.out.println("Width: " + width);
height = scan.nextInt();
System.out.println("Heigth: " + height);
maxValue = scan.nextInt();
fileInputStream.close();
// Now parse the file as binary data
FileInputStream fin = new FileInputStream(fileName);
DataInputStream dis = new DataInputStream(fin);
// look for 4 lines (i.e.: the header) and discard them
int numnewlines = 4;
while (numnewlines > 0) {
char c;
do {
c = (char)(dis.readUnsignedByte());
} while (c != '\n');
numnewlines--;
}
// read the image data
int[][] fileArray = new int[height][width];
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
fileArray[row][col] = scan.nextInt();
System.out.print("(" + row + " ," + col +"): " + fileArray[row][col]+ " ");
}
System.out.println();
}
dis.close();
}
}
any advise would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已关闭
scan
对象正在使用的 InputStream,然后打开另一个。scan
对象耗尽了要读取的整数,这并不奇怪。在关闭输入流之前,它可能会缓冲一些输入流,这就是为什么它完成读取较小文件但读取较大文件时失败的原因。您需要根据稍后打开的新输入流创建一个新的
Scanner
对象。You've closed the InputStream that the
scan
object is using, then opened another one. Not surprising that thescan
object is running out of ints to read. It probably buffers some of the inputstream before you close it, which is why it finishes reading the smaller files but fails on the larger ones.You'll need to make a new
Scanner
object based on the new inputstream you open later.