Tóm Tắt
Overview
Integer.parseInt() and Integer.valueOf() methods can be used to convert String to int in Java. These methods are defined under the Integer class in java.lang package. Both methods throw NumberFormatException when the String input contains characters other than digits.
Scope
This article aims to:
- Explain how to use
Integer.parseInt()
and
Integer.valueOf()
methods.
- Illustrate the
NumberFormatException
thrown by these methods.
Introduction
You are in your computer lab trying to formulate a complex Java equation that will surely be a boon to mankind. Variables are racing in your head, and equations are wandering. You are very close to the solution and require a multiplication to a string.
All you want to do is convert a string to int, multiply the value and submit your equation. The thought of being stuck at the endpoint made you search the web, and you came across this article to convert String to an int in Java.
This article explains two methods of converting a String to an integer in Java:
- Integer.parseInt()
- Integer.valueOf()
Integer.parseInt() method converts String to int (primitive data type) in Java. However, Integer.valueOf() method can be used to convert String to an instance of the Integer class in Java.
Integer.parseInt() – Converts String to int
The parseInt() method converts a String to a primitive (basic) data type, i.e., int in Java. The parseInt() method is a static method of Integer class.
Signature of parseInt() method
public
static
int
parseInt
(String input)
Syntax
int
val = Integer.parseInt(
"200"
);
Example
String str =
"5678"
;
int
value = Integer.parseInt(str);
Let’s see an example of converting String to int in Java.
Code:
class
Example
{
public
static
void
main
(String args[])
{
// String literal
String str =
"123"
;
// Holds the integer equivalent
int
ans = Integer.parseInt(str);
System.out.println(ans); } }
Output:
123
Integer.parseInt(String s, int radix) – Converts String to int
The Integer.parseInt(String s) method uses the default radix as 10 to parse the string input. It means the string input is interpreted as a decimal number while conversion by default.
Specifying the radix input to Integer.parseInt method would set the static method to use it as base number in parsing the input string argument. For example, if the string value is expected to be in hexadecimal format, we would be invoking the method like this:
Integer.parseInt(
"A1E"
,
16
);
// 2590 in base 10
Signature of parseInt(String s, int radix) method
public
static
int
parseInt
(String input,
int
radix)
Syntax
int
val = Integer.parseInt(
"101"
,
2
);
// val = 5
Example
String str =
"1010"
;
int
value = Integer.parseInt(str,
2
);
Let’s see an example of converting String (in a specified base) to int in Java.
Code:
class
Example
{
public
static
void
main
(String args[])
{
// String literal in base 2
// 1011 in base 2 implies 11 in base 10
String str =
"1011"
;
// Holds the integer equivalent i.e. 11
int
ans = Integer.parseInt(str,
2
);
System.out.println(ans); } }
Output:
11
Integer.valueOf() – Converts String to int
Integer.valueOf() method returns the Integer object when a string literal is passed as an argument. It takes in the string or an integer as a first argument and converts it into an Integer object. This works the same as parseInt() and changes a string to an integer in java for the radix argument.
Syntax:
Integer.valueOf(String s)
Integer.valueOf(String s,
int
radix)
// radix = base of String s
Example:
String str =
"12345"
;
Integer change = Integer.valueOf(str);
Code
class
Example
{
public
static
void
main
(String args[])
{
// String literal
String str =
"789"
;
// Holds the integer equivalent assuming base 10 interpretation of str
Integer ans = Integer.valueOf(str);
// Holds the integer equivalent assuming base 16 interpretation of str
Integer ans16 = Integer.valueOf(str,
16
);
System.out.println(ans); System.out.println(ans16); } }
Output:
789
1929
NumberFormatException in Java
If the string literal provided as an argument is ill-formatted, i.e., contains characters other than numerics, NumberFormatException is thrown. Both the Integer.parseInt() and Integer.valueOf() throw NumberFormatException when the string input contains invalid characters.
Code:
class
NumberFormatExceptionExample
{
public
static
void
main
(String args[])
{
// Contains characters that cannot be converted to integers
String str =
"abc123"
;
// throws NumberFormatException
int
ans = Integer.parseInt(str);
System.out.println(ans +
"\n"
);
} }
Output:
Exception in thread
"main"
java.lang.NumberFormatException: For input string:
"abc123"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:
65
)
at java.base/java.lang.Integer.parseInt(Integer.java:
652
)
at java.base/java.lang.Integer.parseInt(Integer.java:
770
)
at Main.main(Main.java:
8
)
Important Points
- The presence of leading zeroes in the string will not be counted by the
parseInt()
as well as the
valueOf()
method.
- The first character as ‘+’ or ‘-’ indicates the sign of the number, which is accepted by both methods of changing the string to integer in java.
Code:
class
Example
{
public
static
void
main
(String args[])
{
// String with leading zeroes
String leadingZero =
"00789"
;
// Strings with a sign
String signConvert =
"-12"
;
int
ans = Integer.parseInt(leadingZero);
int
ans1 = Integer.parseInt(signConvert);
System.out.println(ans);
// outputs 789
System.out.println(ans1);
// outputs -12
} }
Output:
789 12
Conclusion
-
Integer.parseInt()
converts a String to an int whereas
Integer.valueOf()
converts a String to an object of Integer class.
- Null, empty, or non-numeric data in a string literal passed as an argument to
parseInt()
and
valueOf()
methods will throw a
NumberFormatException
.
- Overloaded methods of parseInt() and valueOf() can be used to specify the radix of String representation.