Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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 从文本文件中读取数字不会';行不通_Java_File_File Io - Fatal编程技术网

Java 从文本文件中读取数字不会';行不通

Java 从文本文件中读取数字不会';行不通,java,file,file-io,Java,File,File Io,我试图从下一个文件中读取数字,并计算出文件中最大的数字,但是我得到了错误的答案。文件中有一组数字,用空格分隔,过了一段时间,会生成一行新行。当我检查我的max输出时,我可以看到它越来越小,这是不应该发生的,所以我想这可能与只比较当前行有关?我见过人们将数字添加到列表中,然后我可以对它们进行排序并获取最后一个索引,但为什么这种方法不起作用呢 int max=0; 尝试(扫描仪输入=新扫描仪(文件)){ while(在.hasNextInt()中){ 如果(in.nextInt()>max){ ma

我试图从下一个文件中读取数字,并计算出文件中最大的数字,但是我得到了错误的答案。文件中有一组数字,用空格分隔,过了一段时间,会生成一行新行。当我检查我的
max
输出时,我可以看到它越来越小,这是不应该发生的,所以我想这可能与只比较当前行有关?我见过人们将数字添加到列表中,然后我可以对它们进行排序并获取最后一个索引,但为什么这种方法不起作用呢

int max=0;
尝试(扫描仪输入=新扫描仪(文件)){
while(在.hasNextInt()中){
如果(in.nextInt()>max){
max=in.nextInt();
}
}
}catch(filenotfounde异常){
System.out.println(“未找到文件”);
}
系统输出打印项次(最大值);
每次调用nextInt()方法时,都会从文件中读取一个整数值,因此最后会跳过每一个整数

尝试以下方法:

int value = in.nextInt();

if(value > max) {
    max = value;
}
int value = in.nextInt();

if(value > max) {
    max = value;
}