當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Scanner useRadix()用法及代碼示例


java.util.Scanner類的useRadix(radix)方法將此掃描程序的默認基數設置為指定的基數。掃描儀的基數會影響其默認數字與正則表達式匹配的元素。

用法:

public Scanner useRadix(int 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)



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Scanner useRadix() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。