Java Scanner nextInt() 方法



描述

java Scanner nextInt() 方法扫描下一个标记作为 int。此方法的调用形式 nextInt() 的行为与调用 nextInt(radix) 完全相同,其中 radix 是此扫描仪的默认基数。

声明

以下是 java.util.Scanner.nextInt() 方法的声明

public int nextInt()

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

参数

返回值

此方法返回从输入扫描的 int

异常

  • InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或超出范围

  • NoSuchElementException - 如果输入耗尽

  • IllegalStateException - 如果此扫描仪已关闭

Java Scanner nextInt(int radix) 方法

描述

java.util.Scanner.nextInt(int radix) 方法扫描下一个标记作为 int。如果下一个标记无法转换为下面描述的有效 int 值,则此方法将抛出 InputMismatchException。如果转换成功,则扫描仪将前进到匹配的输入。

声明

以下是 java.util.Scanner.nextInt(int radix) 方法的声明

public int nextInt(int radix)

参数

radix - 用于将标记解释为 int 值的基数

返回值

此方法返回从输入扫描的 int

异常

  • InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或超出范围

  • NoSuchElementException - 如果输入耗尽

  • IllegalStateException - 如果此扫描仪已关闭

在字符串上的 Scanner 获取下一个标记作为 Int 的示例

以下示例演示了如何使用 Java Scanner nextInt() 方法以默认基数扫描下一个标记作为 Int。我们使用给定的字符串创建了一个扫描仪对象。然后我们检查每个标记是否为 Int 并打印,否则打印“未找到”以及扫描的标记。最后,使用 close() 方法关闭扫描仪。

Open Compiler
package com.tutorialspoint; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { String s = "Hello World! 3 + 3.0 = 6"; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); while (scanner.hasNext()) { // check if the scanner's next token is a Int if(scanner.hasNextInt()){ // print what is scanned System.out.println("Found: " + scanner.nextInt()); } else { System.out.println("Not Found: " + scanner.next()); } } // close the scanner scanner.close(); } }

输出

让我们编译并运行上述程序,这将产生以下结果:

Not Found: Hello
Not Found: World!
Found: 3
Not Found: +
Not Found: 3.0
Not Found: =
Found: 6

以给定基数获取下一个标记作为 Int 的 Scanner 字符串示例

以下示例演示了如何使用 Java Scanner nextInt() 方法以给定的基数扫描下一个标记作为 Int。我们使用给定的字符串创建了一个扫描仪对象。然后我们检查每个标记是否为 Int 并打印,否则打印“未找到”以及扫描的标记。最后,使用 close() 方法关闭扫描仪。

Open Compiler
package com.tutorialspoint; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { String s = "Hello World! 3 + 3.0 = 6"; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); while (scanner.hasNext()) { // check if the scanner's next token is a Int if(scanner.hasNextInt(4)){ // print what is scanned System.out.println("Found: " + scanner.nextInt(4)); } else { System.out.println("Not Found: " + scanner.next()); } } // close the scanner scanner.close(); } }

输出

让我们编译并运行上述程序,这将产生以下结果:

Not Found: Hello
Not Found: World!
Found: 3
Not Found: +
Not Found: 3.0
Not Found: =
Not Found: 6

在用户输入上的 Scanner 获取下一个标记作为 Int 的示例

以下示例演示了如何使用 Java Scanner nextInt() 方法以给定的基数扫描下一个标记作为 Int。我们使用 System.in 类创建了一个扫描仪对象。然后我们检查每个标记是否为 Int 并打印,否则打印“未找到”以及扫描的标记。最后,使用 close() 方法关闭扫描仪。

Open Compiler
package com.tutorialspoint; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { // create a new scanner with System Input Scanner scanner = new Scanner(System.in); // check if the scanner's next token is a Int if(scanner.hasNextInt(4)){ // print what is scanned System.out.println("Found: " + scanner.nextInt(4)); } else { System.out.println("Not Found: " + scanner.next()); } // close the scanner scanner.close(); } }

输出

让我们编译并运行上述程序,这将产生以下结果:(我们输入了 3)

3
Found: 3
java_util_scanner.htm
广告