How To Convert String To Int (Integer) In Java?


Java programming language provides different variable types. One of the most used types is String and Int or integer. During the usage of these variable types, we may need to convert them. In this tutorial, we will examine string to int and int to string variables types conversion in Java programming language.

Convert with parseInt() Function

parseInt() is a function that can be used to convert string to int. parseInt() is provided by Integer class which is mainly created to parse and convert different variable types into Integer. In this example, we will create a string type variable named age and then convert to the integer.

String age="20";

int myage= Integer.parseInt(age);

System.out.println(myage);

parseInt() will return a primitive int value which is not an object.

Convert with valueOf() Function

There are an alternative way and function to convert string to int which is named valueOf(). valueOf() function is provided by Integer class and can be used as parseInt() function. In this example, we will create a string type variable named age and then convert to the integer.

String age="20";

int myage= Integer.valueOf(age);

System.out.println(myage);

valueOf() function will return Integer Object which is more complex then primitive integer value.

Convert with decode() Function

Intger class also provides the decode() function. decode() function can be used like other functions where we will provide the string variable to the static decode() function.

String age="20";

int myage= Integer.decode(age);

System.out.println(myage);

Convert with toInt() Function

Another alternative for String to Int conversion is toInt() function .toInt() function is provided by NumberUtils class. We will just provide the string variable or value like below.

String age="20";

int myage= NumberUtils.toInt(age);

System.out.println(myage);

Catch Convert Exceptions

During the convert operation of string to int, we may get exceptions about conversion. If provided string variable contains non-numeric values like a letter, punctuation, etc. the given functions parseInt() and valueOf() will throw an exception about the convert operation. In this example, we will get NumberFormatException because given string provides letter like B.

String age="20B";

try{

   int myage= Integer.valueOf(age);
   System.out.println(myage);

}catch (NumberFormatException e){

   throw e;

}

Valid and Invalid Conversions

There are different possibilities during string to int convert. Here some of them are valid and convert to int and some of the invalid which will not convert into int and throw an exception. We will use parseInt() function but given string values will throw the same error for other string to int conversion functions like decode() , valueOf() , parseInt() .

Integer.parseInt("1"); //OK

Integer.parseInt("-1"); //OK

Integer.parseInt("+1"); //OK

Integer.parseInt(" 1"); //Exception because of space

Integer.parseInt("2147483650"); //Exception integer overflow because an integer can cold up to 2,147,483,647

Integer.parseInt("1.5"); //Exception because  not whole number

Integer.parseInt(""); //Exception no value to convert

LEARN MORE  Powershell Trim Operation Like Filename, String, End, Start, Spaces

Leave a Comment