NoSuchElementException in nextInt() no matter what I do to the code

huangapple go评论95阅读模式
英文:

NoSuchElementException in nextInt() no matter what I do to the code

问题

  1. public class MinimumElement {
  2. public void readIntegers(int userCount) {
  3. int count = userCount;
  4. int intArray[] = new int[count];
  5. Scanner scan = new Scanner(System.in);
  6. for (int i = 0; i <= count - 1; i++) {
  7. int number;
  8. System.out.println("Please input number ");
  9. number = scan.nextInt();
  10. intArray[i] = number;
  11. }
  12. scan.close();
  13. }
  14. public static void main(String[] Args) {
  15. Scanner scan = new Scanner(System.in);
  16. System.out.println("Please enter the number of elements required for array");
  17. int userInput = scan.nextInt();
  18. scan.nextLine();
  19. scan.close();
  20. MinimumElement min = new MinimumElement();
  21. min.readIntegers(userInput);
  22. }
  23. }

已尝试使用hasNextInthasNextLineif条件。它们始终返回false作为结果值。

英文:
  1. public class MinimumElement {
  2. public void readIntegers(int userCount) {
  3. int count = userCount;
  4. int intArray[] = new int[count];
  5. Scanner scan = new Scanner(System.in);
  6. for (int i = 0; i &lt;= count - 1; i++) {
  7. int number;
  8. System.out.println(&quot;Please input number &quot;);
  9. number = scan.nextInt();
  10. intArray[i] = number;
  11. }
  12. scan.close();
  13. }
  14. public static void main(String[] Args) {
  15. Scanner scan = new Scanner(System.in);
  16. System.out.println(&quot;Please enter the number of elements required for array&quot;);
  17. int userInput = scan.nextInt();
  18. scan.nextLine();
  19. scan.close();
  20. MinimumElement min = new MinimumElement();
  21. min.readIntegers(userInput);
  22. }
  23. }

Have tried hasNextInt and hasNextLine with if conditions as well. They are always returning result value as false.

答案1

得分: 0

好的,我相信我可能已经找到了解决方案。问题在于你尝试从System.in读取时的方式:实际上你分配了两个Scanner实例!

  1. int intArray[] = new int[count];
  2. Scanner scan = new Scanner(System.in);

还有这里:

  1. Scanner scan = new Scanner(System.in);
  2. System.out.println("Please enter the number of elements required for array");

这会导致奇怪的问题。因此,改为像下面的示例中所示创建全局的Scanner实例。

  1. public class MinimumElement {
  2. private static final Scanner SCANNER = new Scanner(System.in);
  3. public static void main(String[] args) {
  4. System.out.println("Please enter the number of elements required for array");
  5. try {
  6. int userInput = SCANNER.nextInt();
  7. SCANNER.nextLine();
  8. MinimumElement min = new MinimumElement();
  9. min.readIntegers(userInput);
  10. } finally {
  11. SCANNER.close();
  12. }
  13. }
  14. public void readIntegers(int userCount) {
  15. int[] intArray = new int[userCount];
  16. for (int i = 0; i <= userCount - 1; i++) {
  17. int number;
  18. System.out.println("Please input number ");
  19. number = SCANNER.nextInt();
  20. intArray[i] = number;
  21. }
  22. }
  23. }

请注意,在调用close()后不要与Scanner进行交互,因为这也会导致错误的行为。

英文:

Alright, I believe I might've found a solution to your problem. The issue lies within the way you attempt to read from System.in: you actually allocate two instances of Scanner!

  1. int intArray[] = new int[count];
  2. Scanner scan = new Scanner(System.in);

And over there:

  1. Scanner scan = new Scanner(System.in);
  2. System.out.println(&quot;Please enter the number of elements required for array&quot;);

This will cause weird problems. Therefore, instead create a global instance of Scanner like shown in the example below.

  1. public class MinimumElement {
  2. private static final Scanner SCANNER = new Scanner(System.in);
  3. public static void main(String[] args) {
  4. System.out.println(&quot;Please enter the number of elements required for array&quot;);
  5. try {
  6. int userInput = SCANNER.nextInt();
  7. SCANNER.nextLine();
  8. MinimumElement min = new MinimumElement();
  9. min.readIntegers(userInput);
  10. } finally {
  11. SCANNER.close();
  12. }
  13. }
  14. public void readIntegers(int userCount) {
  15. int[] intArray = new int[userCount];
  16. for (int i = 0; i &lt;= userCount - 1; i++) {
  17. int number;
  18. System.out.println(&quot;Please input number &quot;);
  19. number = SCANNER.nextInt();
  20. intArray[i] = number;
  21. }
  22. }
  23. }

Note that you must take care not to interact with the Scanner after the invoking close(), as that will result in erroneous behavior as well.

huangapple
  • 本文由 发表于 2020年8月9日 17:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63324948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定