+3 votes
62 views
in Tips & Tricks by (242k points)
reopened
Java: Convert strings to integers

1 Answer

+4 votes
by (1.6m points)
 
Best answer

Convert string to integer
Conversion from another base
Possible exception

Do you want to convert a string into an integer in Java? We'll show you how to do this without a lot of effort..

image image

Fortunately, there are already methods in Java for converting a string into an integer. In our instructions, we will show you how to use this function and what problems can arise with it.

Convert string to integer

Java offers you an interface for converting data types of the string type to an integer . The static function parseInt is part of the Integer class . The method is used as follows: The int variable now contains the value 42. You can also use the static method valueOf if you want to create an Integer object instead of an int variable . This method has the same effect, but returns an Integer object.

String numberString = "42";
int numberInt = Integer.parseInt(numberString);



Integer numberInteger = Integer.valueOf(numberString);

Conversion from another base

There may be times when you want to convert a string from a number system other than the tens system. An example of this would be binary or hexadecimal numbers. Fortunately, this can be done by modifying the code slightly. The functions parseInt and valueOf offer the possibility of passing a second parameter in the function call , which determines the base of the number in the string.

String numberString = "101"; // 5 im Binärsystem
int numberInt = Integer.parseInt(numberString, 2); //Als Basis geben wir 2 an

The variable numberInt now contains the value 5, because it was specified in the function call that the number in the numberString is a binary number.

Similarly, we can also create integer objects from a string.

String numberString = "A2D8"; // 41688 im Hexadezimalsystem
Integer numberInteger = Integer.valueOf(numberString, 16); //Als Basis geben wir 16 an

The Integer object numberInteger now receives the value 41688, because the numberString is a hexadecimal number, which we specified with the 16 in the function call.

Possible exception

The methods parseInt and valueOf can both have a NumberFormatException throw , if the arguments do not allow conversion. Consider the following code example:

String numberString = "abc123";
// Wirft eine Exception
int numberInt = Integer.parseInt(numberString);

Here you will find that an attempt to convert " abc123 " to a decimal number will obviously fail. In this case a NumberFormatException is thrown . If you try to convert the same string into the hexadecimal system:

String numberString = "abc123";
// Wirft keine Exception denn abc123 hexadezimal entspricht 1194684 dezimal
int numberInt = Integer.parseInt(numberString, 16);

Accordingly, it makes sense to make sure beforehand whether a conversion can take place at all. In addition to this, you should of course also use exception handling if you cannot ensure that the strings are in the correct format..


...