Scanner useRadix() method in Java with Examples
Last Updated :
10 Oct, 2018
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:
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Geeksforgeeks has Scanner Class Methods" ;
Scanner scanner = new Scanner(s);
System.out.println( "String:\n"
+ scanner.nextLine());
System.out.println( "\nOld Radix: "
+ scanner.radix());
scanner.useRadix( 12 );
System.out.println( "\nNew Radix: "
+ scanner.radix());
scanner.close();
}
catch (IllegalArgumentException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
String:
Geeksforgeeks has Scanner Class Methods
Old Radix: 10
New Radix: 12
Program 2: Exception demonstrated
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Geeksforgeeks has Scanner Class Methods" ;
Scanner scanner = new Scanner(s);
System.out.println( "String:\n"
+ scanner.nextLine());
System.out.println( "\nOld Radix: "
+ scanner.radix());
scanner.useRadix( 64 );
System.out.println( "\nNew Radix: "
+ scanner.radix());
scanner.close();
}
catch (IllegalArgumentException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
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)
Share your thoughts in the comments
Please Login to comment...