Home » Java programming language

Java Scanner useRadix() Method with Example

Scanner Class useRadix() method: Here, we are going to learn about the useRadix() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on February 18, 2020

Scanner Class useRadix() method

  • useRadix() method is available in java.util package.
  • useRadix() method is used to assigns the default or implicit radix (rad) to the given radix of this Scanner.
  • useRadix() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • useRadix() method may throw an exception at the time of set radices.
    IllegalArgumentException: This exception may throw when the given parameter radix (rad) is not in a range.

Syntax:

    public Scanner useRadix(int rad);

Parameter(s):

  • int rad – represents the radix to assign this Scanner object.

Return value:

The return type of the method is Scanner, it returns an object of this Scanner.

Example:

// Java program to demonstrate the example 
// of Scanner useRadix(int rad) method of Scanner 

import java.util.*;

public class UseRadixOfScanner {
    public static void main(String[] args) {
        String str = "Hi, true IncludeHelp! 8 + 2.0f = 10.0f";

        // Instantiate Scanner with the 
        // given str
        Scanner sc = new Scanner(str);

        // Display Scanner
        System.out.println("sc.nextLine(): " + sc.nextLine());

        // By using useRadix() method is to set
        // the radix for this Scanner
        sc.useRadix(20);

        // By using radix() method is to print
        // the radix of this Scanner
        System.out.println("sc.radix(): " + sc.radix());

        // close the scanner
        sc.close();
    }
}

Output

sc.nextLine(): Hi, true IncludeHelp! 8 + 2.0f = 10.0f
sc.radix(): 20


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.