Home > Java > javaTutorial > Use java's Scanner.useRadix() function to set the radix for input parsing

Use java's Scanner.useRadix() function to set the radix for input parsing

王林
Release: 2023-07-25 14:34:49
Original
1320 people have browsed it

Use Java's Scanner.useRadix() function to set the radix for input parsing

In Java programming, we often need to parse numbers from user input. By default, Java's Scanner class parses input into decimal numbers. However, sometimes we want to parse numbers in other bases, such as binary or hexadecimal. At this time, you can use Scanner's useRadix() function to set the base of input parsing.

The Scanner class is a commonly used input processing class in Java. It provides various convenient methods for reading input from various sources (such as standard input, files, etc.). Through Scanner's useRadix() function, we can specify the base of the number to be parsed.

The prototype of the useRadix() function is as follows:

public Scanner useRadix(int ​​radix)

The radix parameter indicates the parsing base to be set, which can be between 2 and 36 any integer between. This base determines how the Scanner parses the input values.

The following is a simple example that demonstrates how to use the useRadix() function to parse binary and hexadecimal numbers:

import java.util.Scanner;

public class RadixExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("请输入一个二进制数: ");
        scanner.useRadix(2); // 设置解析基数为2
        
        int binary = scanner.nextInt();
        System.out.println("解析的结果为: " + binary);
        
        System.out.print("请输入一个十六进制数: ");
        scanner.useRadix(16); // 设置解析基数为16
        
        int hex = scanner.nextInt();
        System.out.println("解析的结果为: " + hex);
        
        scanner.close();
    }
}
Copy after login

The above example first uses the useRadix(2) function to parse the radix Set to binary. Then, read the binary number entered by the user through the nextInt() method. Finally, the parsing results are output to the console.

Next, the example uses the useRadix(16) function to set the parsing base to hexadecimal. Then, read the hexadecimal number entered by the user through the nextInt() method. Finally, the parsing results are output to the console.

You can try entering different binary or hexadecimal numbers and check the parsing results.

To summarize, using Java's Scanner.useRadix() function can easily set the radix of input parsing. By setting different bases, we can flexibly parse numbers in different bases. This is very useful in some specific application scenarios, such as computer science, digital transformation, etc. I hope this article will be helpful in understanding and using the Scanner.useRadix() function.

The above is the detailed content of Use java's Scanner.useRadix() function to set the radix for input parsing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template