Home > Java > javaTutorial > Convert string to integer using java's Integer.parseInt() function

Convert string to integer using java's Integer.parseInt() function

WBOY
Release: 2023-07-25 11:53:23
Original
1395 people have browsed it

Use Java's Integer.parseInt() function to convert a string into an integer

With the continuous development of computer technology, string processing is becoming more and more common in programming. In some cases, we need to convert strings to integers. For Java, you can use the Integer.parseInt() function to achieve this goal.

Integer.parseInt() is a very useful method provided by Java, which can convert a string into an integer. The syntax of this method is as follows:

public static int parseInt(String s) throws NumberFormatException

where s is the string to be converted to an integer. This method will parse the string s into a signed decimal integer. If the string s is not a legal integer (for example, contains characters other than numeric characters), a NumberFormatException will be thrown.

The following is a simple sample code that shows how to use the Integer.parseInt() function:

public class StringToIntExample {
    public static void main(String[] args) {
        String str = "12345";

        try {
            int number = Integer.parseInt(str);
            System.out.println("转换后的整数:" + number);
        } catch (NumberFormatException e) {
            System.out.println("字符串不是一个合法的整数");
        }
    }
}
Copy after login

In the above example, we define a string str and assign it a value to "12345". Then, we call the Integer.parseInt() function to convert the string str to an integer and assign the result to the number variable. Finally, we print out the converted integer.

If the string str cannot be converted to a legal integer, that is, it contains characters other than numeric characters, then the NumberFormatException exception will be thrown. In the exception handling code block, we print out a message that the string is not a legal integer.

It should be noted that when the string str represents a negative number, it can also be converted through the Integer.parseInt() function. For example, you can convert a string like "-12345" into a negative number.

In addition, you can also specify the base represented by the string by specifying the second parameter. For example, a binary string can be converted to an integer by passing the string "1010" to the Integer.parseInt() function along with argument 2.

To summarize, you can easily convert a string into an integer using Java's Integer.parseInt() function. It is one of the common ways to handle string and integer conversion. In actual development, we often encounter situations where we need to convert strings to integers, such as when reading text files, or when processing user input. By mastering the use of the Integer.parseInt() function, we can handle these operations more efficiently.

The above is the detailed content of Convert string to integer using java's Integer.parseInt() function. 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