How to convert string array to int array in java with an example

By Saruque Ahamed Mollick

In this Java tutorial, I will show you how to convert string array to int array in Java. If you wish to convert a string to integer array then you will just need to use parseInt() method of the Integer class. But in this post, you will learn how to convert string array to integer array in Java.



In order to convert a string array to an integer array, I will first convert each element of the string array to integer. Then I will populate the Integer array with those elements.

Also Read,

Guess The Number Game Using Java with Source Code

FizzBuzz Problem In Java- A java code to solve FizzBuzz problem

Convert String Array to Int Array in Java

The below is an easy example, take a look

import java.util.*;

public class MyClass {
   public static void main(String[] args) {
      String [] my_string_array = {"548", "142", "784", "786"};
      int string_array_length = my_string_array.length;
      int []  integer_array = new int [string_array_length];

      for(int i=0; i<string_array_length; i++) {
         integer_array[i] = Integer.parseInt(my_string_array[i]);
      }
      System.out.println(Arrays.toString(integer_array));
   }
}

The output of this program will be like this:

[548, 142, 784, 786]

Explanation:

String [] my_string_array = {"548", "142", "784", "786"};

This line creates an array of string.

The for loop populate the converted integers element one by one.

Finally,

System.out.println(Arrays.toString(integer_array));

Print the result with this line.

I hope this will be helpful to you. If you have any query post that in the below comment section.

Also Read,

How to remove null values from a String array in Java in various ways

how to convert Object array to String array in java

Leave a Reply

Your email address will not be published. Required fields are marked *