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

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

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

问题

public class MinimumElement {

    public void readIntegers(int userCount) {
        int count = userCount;
        int intArray[] = new int[count];
        Scanner scan = new Scanner(System.in);

        for (int i = 0; i <= count - 1; i++) {
            int number;
            System.out.println("Please input number ");
            number = scan.nextInt();
            intArray[i] = number;
        }
        scan.close();
    }

    public static void main(String[] Args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the number of elements required for array");
        int userInput = scan.nextInt();
        scan.nextLine();
        scan.close();
        MinimumElement min = new MinimumElement();
        min.readIntegers(userInput);

    }

}

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

英文:
public class MinimumElement {

public void readIntegers(int userCount) {
	int count = userCount;
	int intArray[] = new int[count];
	Scanner scan = new Scanner(System.in);

	for (int i = 0; i &lt;= count - 1; i++) {
		int number;
		System.out.println(&quot;Please input number &quot;);
		number = scan.nextInt();
		intArray[i] = number;
	}
	scan.close();
}

public static void main(String[] Args) {
	Scanner scan = new Scanner(System.in);
	System.out.println(&quot;Please enter the number of elements required for array&quot;);
	int userInput = scan.nextInt();
	scan.nextLine();
	scan.close();
	MinimumElement min = new MinimumElement();
	min.readIntegers(userInput);

}

}

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

答案1

得分: 0

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

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

还有这里:

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

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

public class MinimumElement {

    private static final Scanner SCANNER = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("Please enter the number of elements required for array");
        try {
            int userInput = SCANNER.nextInt();
            SCANNER.nextLine();
            MinimumElement min = new MinimumElement();
            min.readIntegers(userInput);
        } finally {
            SCANNER.close();
        }
    }

    public void readIntegers(int userCount) {
        int[] intArray = new int[userCount];
        for (int i = 0; i <= userCount - 1; i++) {
            int number;
            System.out.println("Please input number ");
            number = SCANNER.nextInt();
            intArray[i] = number;
        }
    }
}

请注意,在调用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!

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

And over there:

Scanner scan = new Scanner(System.in);
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.

public class MinimumElement {

    private static final Scanner SCANNER = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println(&quot;Please enter the number of elements required for array&quot;);
        try {
            int userInput = SCANNER.nextInt();
            SCANNER.nextLine();
            MinimumElement min = new MinimumElement();
            min.readIntegers(userInput);
        } finally {
            SCANNER.close();
        }
    }

    public void readIntegers(int userCount) {
        int[] intArray = new int[userCount];
        for (int i = 0; i &lt;= userCount - 1; i++) {
            int number;
            System.out.println(&quot;Please input number &quot;);
            number = SCANNER.nextInt();
            intArray[i] = number;
        }
    }
}

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:

确定