How to convert a String into Int in Java
Java is a powerful general-purpose programming language used to develop a wide set of applications, including desktop apps, android applications, embedded applications, etc. It is one of the most popular programming language and has inspired the development of Java child languages, such as Kotlin, Scala, Groovy, Clojure and more.
In you development time, you will often come across instances where you need to convert a string to an integer value. For example, when converting an input to its numerical equivalent.
The purpose of this tutorial is to show you various quick and easy methods you can use to convert a given string into an integer using the Java programming Language.
We recommend you have knowledge of Java Strings and Data Types for maximum comprehension.
Get your IDE ready and a cup of coffee!!
Method 1 - Using Java parseInt() Method
The most common method you can use to convert a string to int is the parseInt()
method from the Integer wrapper class. The function syntax is as shown:
public static int parseInt(String s) throws NumberFormatException
The function one parameter of type String
s, It then returns the int
equivalent of the input string.
An example is as shown:
class stringConversion {
public static void main(String args[]) {
String var = "100";
int var_int = Integer.parseInt(var);
System.out.println(var_int);
}
}
In the example above, we start by defining a variable var
of type string. This holds a numerical value in string format.
We then use the use the parseInt()
method to convert the variable to an int and save the result to variable var_int
.
We can run the code above as:
java string_conversion.java
100
Ensure that the value you wish to convert is a numerical value. If you attempt to convert a non-numerical value, the compiler will throw an exception as shown:
class stringConversion {
public static void main(String args[]) {
String var = "geekbits";
int var_int = Integer.parseInt(var);
System.out.println(var_int);
}
}
In this case, the input value is an alphabetical value and not a numerical value. This forces the compiler to return an exception as shown:
Exception in thread "main" java.lang.NumberFormatException: For input string: "geekbits"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at stringConversion.main(string_conversion.java:4)
The compiler returns a NumberFormatException
.
Method 2 - Using the valueOf()
Function
The second method you can use to convert an int
to a String
is the valueOf
method. This methods takes a given input and returns its string representation.
The function syntax is as shown:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
An example is as illsutrated below:
class StringConversion {
public static void main(String args[]) {
String val = "100";
int val_int = Integer.valueOf(val);
System.out.println(val_int);
}
}
The valueOf()
method returns an Integer
object from the provided input. Keep in mind that the method performs type unboxing.
We can run the code as:
java string_conversion.java
100
Similarly, the method will return an excpetion if the input is not a numerical type.
class StringConversion {
public static void main(String args[]) {
String val = "geekbits`";
int val_int = Integer.valueOf(val);
System.out.println(val_int);
}
}
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "geekbits`"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.valueOf(Integer.java:999)
at StringConversion.main(string_conversion.java:4)
Conlusion
In this post, we discussed how to use the parseInt()
and valueOf()
methods to convert a given string to an integer type in Java.
We hope you enjoyed this tutorial. If you did, leave a comment down below and share.
Thanks for reading and sign up to get articles straight into your inbox when we publish.
Catch you in the next one!!