java.util.NoSuchElementException 多个 while 语句出错

java.util.NoSuchElementException error on multiple while statements

Screenshot of error message

当我 运行 我的代码时出现此错误,请注意它在第 37 行发现问题,但我无法弄清楚它是什么。 运行 扫描仪方法的第一次迭代(对于输入 1)工作正常,并产生了正确的输出,但是连续的 none 有,我一直被这个问题所困扰。代码如下:

import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;

public class Assignment2 {

public static void main(String[] args)  {
        
        int largest= 0;
        int largestEven = 0;
        int countOfAllPositive = 0;
        int sumOfAll = 0;
        
    try {
        Scanner input1 = new Scanner(new File("input1.txt"));
        while (input1.hasNextInt()) { 
            if (input1.nextInt() == 0)
            { break; 
            } else if (input1.nextInt() < largest)
            { largest = input1.nextInt(); 
            } else if (input1.nextInt() > 0)
            { largestEven += input1.nextInt();
            } else if (input1.nextInt() % 2 == 0)
            { countOfAllPositive += input1.nextInt();
            } else if (input1.nextInt() < 0)
            { sumOfAll++;
            }
            
        }
        Scanner input2 = new Scanner(new File("input2.txt"));
        while (input2.hasNextInt()) { 
            if (input2.nextInt() == 0)
            { break; 
            } else if (input2.nextInt() < largest)
            { largest = input2.nextInt(); 
            } else if (input2.nextInt() > 0)
            { largestEven += input2.nextInt();
            } else if (input2.nextInt() % 2 == 0)
            { countOfAllPositive += input2.nextInt();
            } else if (input2.nextInt() < 0)
            { sumOfAll++;
            }
        }
        Scanner input3 = new Scanner(new File("input3.txt"));
        while (input3.hasNextInt()) { 
            if (input3.nextInt() == 0)
            { break; 
            } else if (input3.nextInt() < largest)
            { largest = input3.nextInt(); 
            } else if (input3.nextInt() > 0)
            { largestEven += input3.nextInt();
            } else if (input3.nextInt() % 2 == 0)
            { countOfAllPositive += input3.nextInt();
            } else if (input3.nextInt() < 0)
            { sumOfAll++;
            }
        }
        Scanner input4 = new Scanner(new File("input4.txt"));
        while (input4.hasNextInt()) { 
            if (input4.nextInt() == 0)
            { break; 
            } else if (input4.nextInt() < largest)
            { largest = input4.nextInt(); 
            } else if (input4.nextInt() > 0)
            { largestEven += input4.nextInt();
            } else if (input4.nextInt() % 2 == 0)
            { countOfAllPositive += input4.nextInt();
            } else if (input4.nextInt() < 0)
            { sumOfAll++;
            }
        }
    } catch (FileNotFoundException e) {
    System.out.println("An error occurred.");
    e.printStackTrace(); } 
    
System.out.println("The largest integer in the sequence is " + largest);
System.out.println("The largest even integer in the sequence is " + largestEven);
System.out.println("The count of all positive integers in the sequence is " + countOfAllPositive);
System.out.println("The sum of all integers is " + sumOfAll);
    }
    }
    

在您的案例中,异常 NoSuchElementException 是由于尝试在 file/input 上使用 nextInt() 引起的,该 file/input 没有更多的 int 可供读取,请参阅 Javadoc

Thrown by various accessor methods to indicate that the element being requested does not exist.

你遇到这个问题的原因是每次你调用 nextInt() in 实际上用完了一个 int 并移动到下一个。

所以你在 if/else 中一遍又一遍地调用 nextInt() 而不是这个 if:

if (input1.nextInt() == 0) //Gets the next int
{ break; 
} else if (input1.nextInt() < largest) //Gets the next int (different from the previous)
{ largest = input1.nextInt();  //Gets the next int (different from the previous)
... //And so on

您需要调用 nextInt() 一次并将其分配给一个值然后使用该值:

//Store the int as a value
int value = input1.nextInt();

//Use the value instead of calling "nextInt()" again
if (value  == 0)
{ break; 
} else if (value < largest)
{ largest = value; 
...