[英]How to use Scanner hasNextInt() inside a while loop?
I cannot get out of while loop.我无法退出 while 循环。
I do not why sc.hasNextInt()
does not return false after last read number.我不知道为什么
sc.hasNextInt()
在上次读取数字后不返回 false。
Should I use another method or is there a mistake in my code?我应该使用其他方法还是我的代码中有错误?
public static void main(String[] args) {
// Creating an array by user keyboard input
Scanner sc = new Scanner(System.in);
System.out.println("Length of array: ");
int[] numbers = new int[sc.nextInt()];
System.out.printf("Type in integer elements of array ", numbers.length);
int index = 0;
**while ( sc.hasNextInt()) {**
numbers[index++] = sc.nextInt();
}
// created method for printing arrays
printArray(numbers);
sc.close();
}
When you are receiving your input from the console, the Scanner hasNextInt()
method placed inside a while
loop condition will continue to read (meaning the loop will continue), until one of the following happens:当您从控制台接收输入时,放置在
while
循环条件中的 Scanner hasNextInt()
方法将继续读取(意味着循环将继续),直到发生以下情况之一:
Thus, in your case you cannot have the hasNextInt()
inside your while loop condition - I am showing a solution below with a counter variable that you can use.因此,在您的情况下,您的 while 循环条件中不能有
hasNextInt()
- 我在下面展示了一个解决方案,其中包含一个您可以使用的计数器变量。
However , the hasNextInt()
method inside a while
loop has its practical usage for when reading from a different source than the console - eg from a String or a file.但是,
while
循环中的hasNextInt()
方法在从与控制台不同的源(例如,从字符串或文件)读取时有其实际用途。 Inspired from the examples here , suppose we have:受 这里示例的启发,假设我们有:
String s = "Hello World! 3 + 3.0 = 6 ";
We can then pass the string s
as an input source to the Scanner (notice that we are not passing System.in
to the constructor):然后我们可以将字符串
s
作为输入源传递给 Scanner(注意我们没有将System.in
传递给构造函数):
Scanner scanner = new Scanner(s);
Then loop until hasNext()
, which checks if there is another token of any type in the input.然后循环直到
hasNext()
,它检查输入中是否有任何类型的另一个标记。 Inside the loop, perform a check if this token is an int using hasNextInt()
and print it, otherwise pass the token to the next one using next()
:在循环内,使用
hasNextInt()
检查此标记是否为 int 并打印它,否则使用next()
将标记传递给下一个:
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
System.out.println("Found int value: " + scanner.next());
} else {
scanner.next();
}
}
Result:结果:
Found int value: 3
Found int value: 6
In the example above, we cannot use hasNextInt()
in the while loop condition itself, because the method returns false on the first non-int character that it finds (so the loop closes immediately, as our String begins with a letter).在上面的示例中,我们不能在 while 循环条件本身中使用
hasNextInt()
,因为该方法在它找到的第一个非 int 字符上返回 false(因此循环立即关闭,因为我们的 String 以字母开头)。
However, we could use while (hasNextInt())
to read the list of numbers from a file .但是,我们可以使用
while (hasNextInt())
从文件中读取数字列表。
Now, the solution to your problem would be to place the index
variable inside the while loop condition:现在,解决您的问题的方法是将
index
变量放在 while 循环条件中:
while (index < numbers.length) {
numbers[index++] = sc.nextInt();
}
Or for clarity`s sake, make a specific counter variable:或者为了清楚起见,创建一个特定的计数器变量:
int index = 0;
int counter = 0;
while (counter < numbers.length) {
numbers[index++] = sc.nextInt();
counter++;
}
Do the following:请执行下列操作:
Use the input length as the end of the loop.使用输入长度作为循环的结束。
// Creating an array by user keyboard input
Scanner sc = new Scanner(System.in);
System.out.println("Length of array: ");
int len = sc.nextInt();
int[] numbers = new int[len]; // use len here
System.out.printf("Type in integer elements of array ", numbers.length);
int index = 0;
for (index = 0; index < len; index++) { // and use len here
numbers[index] = sc.nextInt();
}
// created method for printing arrays
printArray(numbers);
sc.close();
And don't close the scanner.并且不要关闭扫描仪。
I cannot get out of while loop.我无法退出 while 循环。
I do not why sc.hasNextInt()
does not return false after last read number.我不知道为什么
sc.hasNextInt()
在上次读取数字后不返回 false。
Should I use another method or is there a mistake in my code?我应该使用另一种方法还是我的代码中有错误?
public static void main(String[] args) {
// Creating an array by user keyboard input
Scanner sc = new Scanner(System.in);
System.out.println("Length of array: ");
int[] numbers = new int[sc.nextInt()];
System.out.printf("Type in integer elements of array ", numbers.length);
int index = 0;
**while ( sc.hasNextInt()) {**
numbers[index++] = sc.nextInt();
}
// created method for printing arrays
printArray(numbers);
sc.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.