Converting a String Array To Int Array in Java

Converting a String Array To Int Array in Java | The string is the collection of a sequence of characters that are immutable which means once it is created they cannot be changed whereas an integer is a data type that takes an only number as input. In this section, we see how to convert string array to int array Java.

For example:
String array: {β€œ89”, β€œ12”, β€œ023”, β€œ458”, β€œ3158”}
int array: {89, 12, 23, 458, 3158}

How to Convert String Array To Int Array Java

To change string array to int array in Java we have to iterate over the String array. It can be done in the following steps:-

1. Take a string array
2. Declare an int array of the same size
3. Iterate the string array
4. Fetch each element of the string array
5. Convert element to a variable
6. Store the value of the variable in the string array

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      String[] stringArr = {"89", "12", "023", "458", "3158"};
      System.out.println("String array: \n" 
             + Arrays.toString(stringArr));
      System.out.println("String array type: " 
             + stringArr.getClass().getName());
      
      // convert string array to int array
      int[] intArr = new int[stringArr.length];
      for (int i = 0; i < stringArr.length; i++) {
         intArr[i] = Integer.valueOf(stringArr[i]);
      }
      
      System.out.println("int array: \n" 
             + Arrays.toString(intArr));
      System.out.println("int array type: " 
             + intArr.getClass().getName());
   }
}

Output:-

String array:
[89, 12, 023, 458, 3158]
String array type: [Ljava.lang.String;
int array:
[89, 12, 23, 458, 3158]
int array type: [I

Convert String Array To Int Array in Java Example-2

In the above example, we have converted the string element to int using the valueOf() method. But if the given string is not parsable then the valueOf() method throws NumberFormatException. In that case, the above program will terminate in the middle. To solve this problem we should use a try-catch block, and add only those elements to the int array which are parsable.

We should not take an int array, and add the result value to it because arrays are fixed in size. In the given string array how many elements are parsable, we don’t know. Therefore it is better to take a list, add elements to the list and later convert the list to an array.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      String[] stringArr = 
         { "89", "Hi", "023", "Hello123", "3158" };
      System.out.println("String array: \n" 
             + Arrays.toString(stringArr));
      System.out.println("String array type: " 
             + stringArr.getClass().getName());
      
      // convert string array to int array
      List<Integer> list = new ArrayList<>();
      for (int i = 0; i < stringArr.length; i++) {
         try {
            list.add(Integer.valueOf(stringArr[i]));
         } catch(NumberFormatException nfe) {}
      }
      
      // convert list to array
      Integer[] intArr = list.toArray(new Integer[] {});
      
      System.out.println("int array: \n" 
          + Arrays.toString(intArr));
      System.out.println("int array type: " 
          + intArr.getClass().getName());
   }
}

Output:-

String array:
[89, Hi, 023, Hello123, 3158]
String array type: [Ljava.lang.String;
int array:
[89, 23, 3158]
int array type: [Ljava.lang.Integer;

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.