JAVA String to Int Complete Guide JAVA 8

You can an integer that is present in form of a string and you do not know how to convert string to int in JAVA. Then this tutorial is for you.

In any case, we want to convert a String of Integer to an Integer or an Array of Integer. So, let us look at how we can convert JAVA String to Int with code examples.

JAVA String to Int

Initialization of String

In JAVA, we can initialize a String Object as shown below. Please keep in mind that all of the methods are related to JAVA 8. let’s initialize the String Object to the value 123456.

String numString = new String("123456");

Now we’d like to convert the above string of numbers into integers. So let’s take a look at a complete example.

Static Method parseInt() of Integer Class

The static method parseInt in the Integer class is used to convert strings to integers. This is a built-in function for converting a string stream to 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);
	}
}

It should be noted that if there are no numbers in the String variable, you will receive a NumberFormatException Error. As a result, when dealing with it, ensure that String only contains numbers.

Alternatively, you can try to capture the conversion in a try-catch statement to catch the exception when the string contains no integers.

Static Method valueOf() of Integer Class

Another method defined in the Integer Class to convert String to int is valueOf. So, using a simple example code, let’s learn how to use valueOf.

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

Another issue that you may encounter is converting a space-separated String of numbers to an Integer Array. How do we convert a String of numbers to an Integer Array? For a better understanding, let’s look at the example code.

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 used the split() method to get the String separated by numbers and then stored it in a string array. After that, we went through each string in a string array and converted it to an integer, which we then stored in an array.

The For Loop in the preceding code adheres to JAVA 8 standards, but we can also use a standard normal loop-through method for ease of understanding if you are a newbie.

So we can do the conversion of String this way into an integer. The same method can also be used to convert a String to a double.

So that’s it for today. If you have any suggestions or queries feel free to comment. Also, like my Facebook page and follow me on Twitter.

Further Read:

  1. Java HashMap Code Examples

Leave a Comment