扫描器类的nextInt()方法 (Scanner Class nextInt() method)
Syntax:
句法:
public int nextInt();
public int nextInt(int rad);
nextInt() method is available in java.util package.
nextInt()方法在java.util包中可用。
nextInt() method is used to read the next token of the input as an int value at the implicit radix (rad) of this Scanner.
nextInt()方法用于读取输入的下一个标记,作为此Scanner的隐式基数(rad)处的int值。
nextInt(int rad) method is used to read the next token of the input as an int value at the explicit or given radix (rad) of this Scanner.
nextInt(int rad)方法用于读取输入的下一个标记作为此Scanner的显式或给定基数(rad)的int值。
These methods may throw an exception at the time of representing input as an int.
在将输入表示为int时,这些方法可能会引发异常。
- InputMismatchException: This exception may throw when the next token of the input mismatch.InputMismatchException :当输入的下一个标记不匹配时,可能引发此异常。
- NoSuchElementException: This exception may throw when no such element exists.NoSuchElementException :如果不存在这样的元素,则可能引发此异常。
- IllegalStateException: This exception may throw when this Scanner is not opened.IllegalStateException :如果未打开此扫描器,则可能引发此异常。
These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象访问;如果尝试使用类名称访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, nextInt(),
在第一种情况下, nextInt() ,
- It does not accept any parameter.
In the second case, nextInt(int rad),
在第二种情况下, nextInt(int rad) ,
- int rad – represents the radix used to manipulate the token as an int.
- int rad –表示用于将标记作为int操作的基数。
Return value:
返回值:
In both the cases, the return type of the method is int, it retrieves the int read from the input.
在这两种情况下,方法的返回类型都是int ,它检索从输入读取的int。
Example 1:
范例1:
// Java program is to demonstrate the example
// of nextInt() of Scanner
import java.util.*;
import java.util.regex.*;
public class NextInt {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24 + b";
byte b = 10;
// Instantiates Scanner
Scanner sc = new Scanner(str);
while (sc.hasNext()) {
// By using nextInt() method isto
// return the next token as a
// int in the default radix
if (sc.hasNextInt()) {
int next_i = sc.nextInt();
System.out.println("sc.nextInt()): " + next_i);
}
System.out.println(sc.next());
}
// Scanner closed
sc.close();
}
}
Output
输出量
Java
Programming!
sc.nextInt()): 3
*
8=
sc.nextInt()): 24
+
b
Example 2:
范例2:
import java.util.*;
import java.util.regex.*;
public class NextInt {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24 + b";
byte b = 10;
// Instantiates Scanner
Scanner sc = new Scanner(str);
while (sc.hasNext()) {
// By using nextInt(9) method isto
// return the next token as a
// int in the given radix
if (sc.hasNextInt()) {
int next_i = sc.nextInt(9);
System.out.println("sc.nextInt(9)): " + next_i);
}
System.out.println(sc.next());
}
// Scanner closed
sc.close();
}
}
Output
输出量
Java
Programming!
sc.nextInt(9)): 3
*
8=
sc.nextInt(9)): 22
+
b
翻译自: https://www.includehelp.com/java/scanner-nextint-method-with-example.aspx