Java - convert String to int
The best way to convert String to int in java:
xxxxxxxxxx
String text = "123";
int number = Integer.parseInt(text);
System.out.println(number); // 123
Other short solutions:
xxxxxxxxxx
// solution 2
Integer number = Integer.valueOf("123"); // 123
// solution 3
Integer number = new Integer("123"); // 123
// solution 4
Integer number = Integer.decode("123"); // 123
// solution 5 - with Guava
Integer number = Ints.tryParse("123"); // 123
In this post we will cover couple of different ways how to convert String to int or Integer.
Convert String "123" to primitive int with Integer.parseInt()
xxxxxxxxxx
public class Example1 {
public static void main(String[] args) {
String text = "123";
int number = Integer.parseInt(text);
System.out.println(number); // 123
}
}
Output:
xxxxxxxxxx
123
Convert String "123" to Integer object with Integer.valueOf()
xxxxxxxxxx
public class Example2 {
public static void main(String[] args) {
String text = "123";
Integer number = Integer.valueOf(text);
System.out.println(number); // 123
}
}
Output:
xxxxxxxxxx
123
xxxxxxxxxx
public class Example3 {
public static void main(String[] args) {
String text = "123";
Integer number = new Integer(text);
System.out.println(number); // 123
}
}
Output:
xxxxxxxxxx
123
xxxxxxxxxx
public class Example4 {
public static void main(String[] args) {
String text = "123";
Integer number = Integer.decode(text);
System.out.println(number); // 123
}
}
Output:
xxxxxxxxxx
123
xxxxxxxxxx
import com.google.common.primitives.Ints;
public class Example5 {
public static void main(String[] args) {
String text = "123";
Integer number = Ints.tryParse(text);
System.out.println(number); // 123
}
}
Output:
xxxxxxxxxx
123
Both methods:
Integer.parseInt("123")
Integer.valueOf("123")
accepts plus sign '+' and minut sign '-'.
Code example:
xxxxxxxxxx
public class Example6 {
public static void main(String[] args) {
// Integer.parseInt() - returns int
System.out.println(Integer.parseInt("123")); // 123
System.out.println(Integer.parseInt("+123")); // 123
System.out.println(Integer.parseInt("-123")); // -123
// Integer.valueOf() - returns Integer
System.out.println(Integer.valueOf("123")); // 123
System.out.println(Integer.valueOf("+123")); // 123
System.out.println(Integer.valueOf("-123")); // -123
}
}
Output:
xxxxxxxxxx
123
123
-123
123
123
-123
xxxxxxxxxx
public class Example7 {
public static void main(String[] args) {
String text = "123a";
int number = Integer.parseInt(text);
System.out.println(number);
}
}
Output:
xxxxxxxxxx
Exception in thread "main" java.lang.NumberFormatException: For input string: "123a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
Both methods:
Integer.parseInt("123")
Integer.valueOf("123")
will throw java.lang.NumberFormatException
if we try to parse anything else then:
digits or '+', '-' as first character.
As stated in java docs of Integer.parseInt()
The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.
Throws:
NumberFormatException - if the string does not contain a parsable integer
We can convert string to integer by implementing our own atoi() method. ATOI stands for ASCII to integer. Take a look at this atoi java implementation:
ASCII to int - Java implementation