Java Scanner useRadix()方法及示例

Java Scanner useRadix()方法及示例

java.util.Scanner 类的 useRadix(radix) 方法将该扫描器的默认radix设置为指定的radix。一个扫描器的radix会影响其默认数字匹配正则表达式的元素。

语法

public Scanner useRadix(int radix)

参数: 该函数接受一个强制性参数radix,它指定了扫描数字时要使用的radix。

返回值: 该函数返回这个扫描器对象。

异常: 如果radix小于Character.MIN_RADIX或大于Character.MAX_RADIX,那么将抛出IllegalArgumentException。

下面的程序说明了上述函数。

程序1 :

// Java program to illustrate the
// useRadix() method of Scanner class in Java
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            String s = "Geeksforgeeks has Scanner Class Methods";
  
            // create a new scanner
            // with the specified String Object
            Scanner scanner = new Scanner(s);
  
            // print the line of the scanner
            System.out.println("String:\n"
                               + scanner.nextLine());
  
            // display the Old radix
            System.out.println("\nOld Radix: "
                               + scanner.radix());
  
            // change the radix
            // of the scanner to 12
            scanner.useRadix(12);
  
            // display the new radix
            System.out.println("\nNew Radix: "
                               + scanner.radix());
  
            // close the scanner
            scanner.close();
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出:

String:
Geeksforgeeks has Scanner Class Methods

Old Radix: 10

New Radix: 12

程序2: 展示的例外情况

// Java program to illustrate the
// useRadix() method of Scanner class in Java
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            String s = "Geeksforgeeks has Scanner Class Methods";
  
            // create a new scanner
            // with the specified String Object
            Scanner scanner = new Scanner(s);
  
            // print the line of the scanner
            System.out.println("String:\n"
                               + scanner.nextLine());
  
            // display the Old radix
            System.out.println("\nOld Radix: "
                               + scanner.radix());
  
            // change the radix
            // of the scanner to 64
            scanner.useRadix(64);
  
            // display the new radix
            System.out.println("\nNew Radix: "
                               + scanner.radix());
  
            // close the scanner
            scanner.close();
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出:

String:
Geeksforgeeks has Scanner Class Methods

Old Radix: 10
Exception thrown : java.lang.IllegalArgumentException: radix:64

**参考资料: ** https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useRadix(int)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程