Java String to int conversion by parseInt and valueOf methods

The issue of string to integer or float (number) conversion is quite common as doing programming in different programming languages and so in Java. For example, as taking the user input for numbers, the default is taken as strings.

In order to perform mathematical operations or use string numeric characters as numbers in your program, you have to convert the strings into numbers. However, that string must be the int representation that needs to be parsed.

Example of parseInt valueOf example

Java parseInt method – First solution

Java comes up with built-in functions to let us convert strings to int. The parseInt method converts a string into the Java primitive int type. The syntax of using the parseInt method is:

public static int parseInt(String s)

  • The parseInt method parses the given string as a signed decimal integer.
  • All characters in the string argument must be decimal digits.
  • Otherwise, the NumberFormatException exception is raised.
  • The only allowed character is the minus sign (-).

The parseInt method can also be used as follows:

parseInt(String s, int radix)

The second argument specifies the radix. In this way, the given string is parsed as a signed integer in the radix.

An example of parseInt method

The following example shows how to use the parseInt method for converting a string to an int.

See online demo and code

Java parseInt

You may also use the parseLong method if the string is a large number that is beyond the range of int type.

What if a string is float number?

If a Java string to be converted into numbers is a float (e.g. 123.456) then you may use parseFloat and for larger number parseDouble method. See the following demo where not only numbers are converted to float but also used for multiplication with another number:

See online demo and code

 

The output:

parseFloat

What if a string contains non-numeric?

As mentioned earlier, the parseInt Java method throws an error if the given string contains non-numeric number or characters other than numbers and (-) sign. This is shown in the example below:

See online demo and code

parseInt NumberFormat

You see, even the given string contains number; however, it also contains other characters. So an error is thrown which is NumberFormatException.

Second solution – Using the valueOf Java method

You may also use valueOf method for converting the string into integer. The valueOf is the method of Integer class.

This is how the valueOf method can be used with variations:

First way:

valueOf(String s)

  • The valueOf method returns the Integer instance that holds the given string “s” value.
  • Just like the parseInt method, the string argument is interpreted as signed decimal integer.
  • A NumberFormatException exception is thrown if string could not be converted to Integer. For example, the given string contains characters other than numbers with the exception of (-) sign.
  • The (-) represents the negative number.

Second way:

valueOf(int i)

  • In this case, the given primitive int is converted to integer object.

Third way:

valueOf(String s, int radix)

  • The valueOf method returns an Integer object where the second argument is radix.

See the following examples of using the valueOf method.

The example of converting string to integer by valueOf

For this example, two Integer objects are created and assigned the string values; one with negative and other with a positive number in the string:

See online demo and code

The output:

java valueOf

The string contains other characters than number example

For this example, I have used the scanner class for taking the input from the user. The next() method of the scanner class takes the input as string and it is stored in a String object.

This string object is passed to the valueOf method. Now, I “accidentally” entered a non-numeric character to convert this into Integer. See what it outputs:

See online demo and code

string to int conversion

As mentioned earlier, the valueOf Java method throws an exception if a non-numeric character other than (-) is assigned in the string argument.

Converting string to float and double by valueOf

You may use the following syntax for returning the float and double by using the valueOf method:

Float.valueOf(str)

Double.valueOf(str)

See a demonstration below:

See online demo and code

The output as I entered the values for float and double:

valueOf float double

You can see, both string entries are converted to float and double, respectively.

Conclusion:

Both parseInt and valueOf methods can be used for converting int to strings in Java programs. The parseInt method returns primitive int type while valueOf returns Integer object.