JAVA String to Int Complete Guide JAVA 8

We can most of the times convert JAVA String to Int or Integer. So in this tutorial will see all the methods and examples to convert JAVA String to Int, JAVA String to int array or double or in any kind of value.

JAVA String to Int
JAVA String to Int

Obliviously we are looking to convert a String of Integer into Integer or Array of Integer in any scenario. So, Let dive into it that how can we convert JAVA String to Int with Examples

Initialization of String

We can initialize a String Object in JAVA as mentioned below. Note: All the methods are related to JAVA 8. So Lets Initialize String Object with value 123456.

String numString = new String("123456");

Now we would like to convert the above string of numbers into integers which are required maybe for some calculation in your program ahead. So let look into a complete example.

Static Method parseInt() of Integer Class

To convert JAVA string to int we have a static method defined in Integer class named parseInt. This is an inbuilt function to convert a stream of string into an integer.

Example

public class Solution
{
	public static void main(String[] args) {
      String numString = new String("123457");
      //This will print the String number as 123457
      System.out.println(numString);
      
      //Now lets create a integer variable num and add 20 to it 
      // to make sure that we are able to do airthmetic operations 
      int num = Integer.parseInt(numString);
      num = num + 20;
      
      //This will print the String number as 123477
      System.out.println(num);
	}
}

Note that if you do not have numbers in String variable then you will get NumberFormatException Error. Hence to handle it make sure you have taken care that String only contains numbers.

Static Method valueOf() of Integer Class

valueOf is another method defined in Integer Class to convert JAVA String to int. So let’s understand the use of valueOf by Simple Example Code. As it will provide a clearer picture.

public class Solution
{
	public static void main(String[] args) {
      String numString = new String("123457");
      //This will print the String number as 123457
      System.out.println(numString);
      
      //Now lets create a integer variable num and add 20 to it 
      // to make sure that we are able to do airthmetic operations 
      int num = Integer.valueOf(numString);
      num = num + 20;
      
      //This will print the String number as 123477
      System.out.println(num);
	}
}

JAVA String to Int Array

Now another problem which may face is to get a spaced separated String of number to be converted to Integer Array. How can we convert JAVA String to Int Array? To find it let’s see example code for better understanding.

public class Solution
{
	public static void main(String[] args) {
      String numString = new String("1 2 3 4 5 6 7");
      
      //Seperate the number by splitting using spaces
      String[] numStringArray = numString.split(" ");
      
      //Now numStringArray = {"1","2","3","4","5","6","7"}
      //To get these number converted to integer array 
      int[] intArray = new int[numStringArray.length];
      
      //Now Loop Through all the elements of numStringArray and Use
      //parseInt() method to convert and store them in intArray
      int i = 0;
      for(String currNum:numStringArray){
        intArray[i++] = Integer.parseInt(currNum);
      }
      
      //Print all the numbers store in intArray
      for(int num:intArray)
        System.out.println(num);
	}
}

So as seen above we first used the method split() to get the String separated with numbers and then we stored it in a string array. After that, we ran through each of string stored in a string array and converted it into integer and then we stored that integer into array.

The For Loop used in above code corresponds to JAVA 8 standards and we can also use a typical normal loop through method for easier understanding if you are a newbie.

So this way we are able to do JAVA String to int or double conversion. Also, the same method can be followed for JAVA String to double conversion.

So that’s it for today. If you have any suggestions or query feel free to comment. Also, like my Facebook page and follow me on Twitter. To read more of the JAVA Related post you can start over here.

Share your love

One comment

Leave a Reply