英文:
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);
}
}
已尝试使用hasNextInt
和hasNextLine
与if
条件。它们始终返回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 <= 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);
}
}
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("Please enter the number of elements required for array");
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("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;
}
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论