Java Scanner useRadix(int radix) method example

HQ ┬╗ Java Tutorial ┬╗ java.util ┬╗ Scanner ┬╗ useRadix(int radix) method example
Java Scanner useRadix(int radix) method example 2018-02-20T12:52:03+00:00

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.

Sample Output :

Running the delimiter() method example source code of Scanner class will give you the following output

java scanner useRadix(radix) method example

java scanner useRadix(radix) method example

Exception Scenario :

Similar Method :

  • N/A

Suggested Reading List :

References :

 

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close