El método useRadix(radix) de la clase java.util.Scanner establece la base predeterminada de este escáner en la base especificada. La raíz de un escáner afecta a los elementos de su número predeterminado que coincide con las expresiones regulares.
Sintaxis:
public Scanner useRadix(int radix)
Parámetros: la función acepta un parámetro radix obligatorio que especifica el radix que se utilizará al escanear números.
Valor de retorno: la función devuelve este objeto de escáner.
Excepciones: si la base es menor que Character.MIN_RADIX o mayor que Character.MAX_RADIX , se genera una IllegalArgumentException .
Los siguientes programas ilustran la función anterior:
Programa 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
Programa 2: Excepción demostrada
// 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
Referencia: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useRadix(int)