- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包扩展
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
Java.util.Scanner.nextInt(int radix) 方法
描述
java.util.Scanner.nextInt() 方法将输入的下一个标记扫描为一个 int 类型的值。如果下一个标记不能像下面描述的那样转换为有效的 int 值,则此方法将抛出 InputMismatchException 异常。如果转换成功,扫描程序将跳过匹配的输入。
声明
以下是 java.util.Scanner.nextInt() 方法的声明
public int nextInt(int radix)
参数
radix − 用于将标记解释为 int 值的基数
返回值
此方法返回从输入中扫描的 int 值
异常
InputMismatchException − 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围
NoSuchElementException − 如果输入已耗尽
IllegalStateException − 如果此扫描程序已关闭
示例
以下示例显示了 java.util.Scanner.nextInt() 方法的使用。
package com.tutorialspoint; import java.util.*; public class ScannerDemo { public static void main(String[] args) { String s = "Hello World! 3 + 3.0 = 6.0 true "; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); // find the next int token and print it // loop for the whole scanner while (scanner.hasNext()) { // if the next is a int, print found and the int with radix 4 if (scanner.hasNextInt()) { System.out.println("Found :" + scanner.nextInt(4)); } // if no int is found, print "Not Found:" and the token 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.0 Not Found :true
java_util_scanner.htm
广告