Java Scanner nextInt()方法及示例
java.util.Scanner 类的 nextInt(radix) 方法将输入的下一个标记扫描为Int。如果翻译成功,扫描器将前进到匹配的输入。如果没有传递参数radix,那么它的行为与nextInt(radix)类似,radix被假定为默认radix。
语法
public int nextInt()
参数: 该函数接受一个参数 radix ,用于将令牌解释为一个Int值。
返回值: 该函数返回从输入中 扫描出的Int 值。
异常: 该函数抛出三种异常,如下所述。
- InputMismatchException: 如果下一个标记与Integer正则表达式不匹配,或者超出了范围。
- NoSuchElementException: 如果输入已经用完,则抛出。
- IllegalStateException: 如果该扫描器被关闭,则抛出。
下面的程序说明了上述功能。
程序1 :
// Java program to illustrate the
// nextInt() method of Scanner class in Java
// without parameter
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "Gfg 9 + 6 = 12.0";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
while (scanner.hasNext()) {
// if the next is a Int,
// print found and the Int
if (scanner.hasNextInt()) {
System.out.println("Found Int value :"
+ scanner.nextInt());
}
// if no Int is found,
// print "Not Found:" and the token
else {
System.out.println("Not found Int value :"
+ scanner.next());
}
}
scanner.close();
}
}
输出:
Not found Int value :Gfg
Found Int value :9
Not found Int value :+
Found Int value :6
Not found Int value :=
Not found Int value :12.0
程序2
// Java program to illustrate the
// nextInt() method of Scanner class in Java
// with parameter
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "Gfg 9 + 6 = 12.0";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
while (scanner.hasNext()) {
// if the next is a Int,
// print found and the Int
if (scanner.hasNextInt()) {
System.out.println("Found Int value :"
+ scanner.nextInt(12));
}
// if no Int is found,
// print "Not Found:" and the token
else {
System.out.println("Not found Int value :"
+ scanner.next());
}
}
scanner.close();
}
}
输出:
Not found Int value :Gfg
Found Int value :9
Not found Int value :+
Found Int value :6
Not found Int value :=
Not found Int value :12.0
程序3: 演示InputMismatchException
// Java program to illustrate the
// nextInt() method of Scanner class in Java
// InputMismatchException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Gfg 9 + 6 = 12.0";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
while (scanner.hasNext()) {
// if the next is a Int,
// print found and the Int
// since the value 60 is out of range
// it throws an exception
System.out.println("Next Int value :"
+ scanner.nextInt());
}
scanner.close();
}
catch (Exception e) {
System.out.println("Exception thrown: " + e);
}
}
}
输出:
Exception thrown: java.util.InputMismatchException
程序4: 演示NoSuchElementException
// Java program to illustrate the
// nextInt() method of Scanner class in Java
// NoSuchElementException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Gfg";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// Trying to get the next Int value
// more times than the scanner
// Hence it will throw exception
for (int i = 0; i < 5; i++) {
// if the next is a Int,
// print found and the Int
if (scanner.hasNextInt()) {
System.out.println("Found Int value :"
+ scanner.nextInt());
}
// if no Int is found,
// print "Not Found:" and the token
else {
System.out.println("Not found Int value :"
+ scanner.next());
}
}
scanner.close();
}
catch (Exception e) {
System.out.println("Exception thrown: " + e);
}
}
}
输出:
Not found Int value :Gfg
Exception thrown: java.util.NoSuchElementException
程序5: 演示IllegalStateException
// Java program to illustrate the
// nextInt() method of Scanner class in Java
// IllegalStateException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Gfg 9 + 6 = 12.0";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// close the scanner
scanner.close();
System.out.println("Scanner Closed");
System.out.println("Trying to get "
+ "next Int value");
while (scanner.hasNext()) {
// if the next is a Int,
// print found and the Int
if (scanner.hasNextInt()) {
System.out.println("Found Int value :"
+ scanner.nextInt());
}
// if no Int is found,
// print "Not Found:" and the token
else {
System.out.println("Not found Int value :"
+ scanner.next());
}
}
}
catch (Exception e) {
System.out.println("Exception thrown: " + e);
}
}
}
输出:
Scanner Closed
Trying to get next Int value
Exception thrown: java.lang.IllegalStateException: Scanner closed
**参考资料: ** https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt(int)