Java Convert Number String to Array

Java Convert Number String to Array | A string is a sequence of characters, which is actually an object which has several methods that allow performing a particular operation on a string. The string is immutable that is once created it cannot be changed. The array is a collection of similar data elements In a contiguous memory allocation.

Here we discuss Java convert string of numbers to int array. Let us understand it through some examples:-

Example-1:-
1) String: “1 2 3 3 4 4 5 6 7”
array : [1, 2, 3, 4, 4, 5, 6, 7]

Example-2:-
2) String: “78 78 78 9 645”
Array: [78, 78, 78, 9, 645]

There are no particular methods to directly convert string to array so to convert the string to the array we can take the help of the below methods which are available in Java string class:
1) split() method
2) replaceAll() method

Java Convert String to Int Array

The below program convert string to int array java by using the replaceAll() method.

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      String string = "[00,45,89,33,45]";

      String[] strArray = string.replaceAll("\\[", "")
                                .replaceAll("]", "").split(",");
      int[] array = new int[strArray.length];

      for (int i = 0; i < strArray.length; i++) {
         array[i] = Integer.valueOf(strArray[i]);
      }

      System.out.println("Integer array : " + Arrays.toString(array));
   }
}

Output:

Integer array : [0, 45, 89, 33, 45]

In the above java convert number string to array program, the string contains “[” and “]” characters so first we removed them using the replaceAll() method of the Java string class. Later we split the resulting string based on the comma “,”. It gives an array of strings. We created an array of int values with the size of the string array. Now, we iterated through the string array, converted the string value to the int value, and stored it in the int array.

Java Convert String Of Numbers To Int Array

Let us see another program to demonstrate how to convert string to int array in Java by using the split() method.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
   public static int[] stringToArray(String str) {
      String[] splitArr = str.split(" ");
      int[] arr = new int[splitArr.length];
      for (int i = 0; i < splitArr.length; i++) {
         arr[i] = Integer.parseInt(splitArr[i]);
      }
      return arr;
   }

   public static void main(String[] args) throws IOException {
      BufferedReader br = 
         new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter a string of numbers " + 
                         "(separated by space): ");
      String string = br.readLine();

      int[] arr = stringToArray(string);
      System.out.println("Integer array : " + 
                         Arrays.toString(arr));
   }
}

Output:-

Enter a string of numbers (separated by space):
14 12 456 7896
Integer array : [14, 12, 456, 7896]

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.