Scanner useRadix() method in Java with Examples

The useRadix(radix) method of java.util.Scanner class sets this scanner’s default radix to the specified radix. A scanner’s radix affects elements of its default number matching regular expressions.

Syntax:

public Scanner useRadix(int radix)

Parameters: The function accepts a mandatory parameter radix which specifies the radix to use when scanning numbers.

Return Value: The function returns this scanner object.

Exceptions: If the radix is less than Character.MIN_RADIX or greater than Character.MAX_RADIX, then an IllegalArgumentException is thrown.



Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

String:
Geeksforgeeks has Scanner Class Methods

Old Radix: 10

New Radix: 12

Program 2: Exception demonstrated

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

String:
Geeksforgeeks has Scanner Class Methods

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

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useRadix(int)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.