java.util.Scanner useRadix(int radix)
Description :
This java tutorial shows how to use the useRadix(int radix) method of Scanner class of java.util package. This method overrides the default radix of this Scanner object. The radix parameter is very important in dealing with numbers.
Method Syntax :
public Scanner useRadix(int radix)
Parameter Input :
DataType | Parameter | Description |
---|---|---|
int | radix | this will override the default radix used by this Scanner object |
Method Returns :
This method returns this Scanner object with the radix overridden by the method parameter radix.
Compatibility Version :
Requires Java 1.5 and up
Exception :
IllegalArgumentException
The IllegalArgumentException will be thrown if the following scenario has been encountered:
- radix method argument is less than Character.MIN_RADIX
- radix is greater than Character.MAX_RADIX
Discussion :
The Scanner useRadix(radix) class sets the radix to be used by the Scanner object in dealing with numbers. The default radix if it’s not been set, will be 10 which is the counting number base or in short decimal. For example if we set a radix value of 16, then we are dealing with numbers in hexadecimal form. This is necessary to set if we are scanning only a specific number base.
The method useRadix() alone will not be useful. It will be used further on calling other methods of Scanner class such as the nextInt(). The nextIn() if the radix has not been used will be scanning for numbers at base 10 however if for example we set the radix by calling the method useRadix(), then the scanner will be getting the numbers on specific base number.
Looking at the source code of this method, invoking the useRadix(radix) method, will override the default radix setting. However the reset() method is available to revert to the initial setting of the Scanner object. This includes setting with radix, locale and delimiter.
Java Code Example :
This java example source code demonstrates the use of useRadix() method of Scanner class. A sequence of hexadecimal was declared on the constructor of the Scanner object. A new radix was introduced by callin the useRadix() method with parameter 16 which means we intended to read numbers of base 16 or hexadecimal.
package com.javatutorialhq.java.tutorial.scanner; import java.util.Scanner; /* * This is an example source code that shows the use of useRadix(radix) * method to tokenize hexadecimal numbers */ public class ScannerRadixDemo { public static void main(String[] args) { // Initialize Scanner object Scanner scan = new Scanner("A E F 1 3"); // Printing the delimiter used System.out.println("Delimiter:"+scan.delimiter()); scan.useRadix(16); // Print the radix the Scanner object is using System.out.println("Radix:"+scan.radix()); // Printing the tokenized Strings while(scan.hasNextInt()){ System.out.println(scan.nextInt()); } // closing the scanner stream scan.close(); } }
Sample Output :
Running the delimiter() method example source code of Scanner class will give you the following output
Exception Scenario :
Exception in thread "main" java.lang.IllegalArgumentException: radix:255 at java.util.Scanner.useRadix(Unknown Source) at com.teknoscope.java.tutorial.scanner.ScannerRadixDemo.main(ScannerRadixDemo.java:19)
Similar Method :
- N/A
Suggested Reading List :
References :