Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaJava Program to Convert String to Integer Array

Java Program to Convert String to Integer Array

Strings – Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are also a type of exceptional array that holds characters, therefore, strings are immutable as well. 

Integer Array – An array is a combination of the same type of variables. Likewise, a collection of integers (int) referred to by a common name is an Integer Array.

Problem Statement – Given a string, the task is to convert that string into an integer array

Let us first go through few examples to make it more clear.

Examples:

Input  : String : "1 23 456 7890"
Output : Integer array : [1 23 456 7890]
Input  : String : "[1,2,356,678,3378]"
Output : Integer array : [1, 2, 356, 678, 3378]

There are numerous approaches to do the same; a few of them are listed below.

Methods:

  1. Using string.replaceAll() method
  2. Using string.split() method

Method 1 – Using string.replaceAll() method  

The string.replaceAll() method takes two arguments, a regex, and the replacement values. This method will replace the given regex with the given replacement value, and after that, the split() method is used to split the string.

Java




// Java Program to Convert String to Integer Array
// Using string.replaceAll() method
 
// Importing input output and utility classes
import java.io.*;
import java.util.Arrays;
 
public class GFG {
    public static void main(String[] args)
    {
        // declaring the string
        String str = "[1,2,356,678,3378]";
 
        // calling replaceAll() method and split() method on
        // string
        String[] string = str.replaceAll("\\[", "")
                              .replaceAll("]", "")
                              .split(",");
 
        // declaring an array with the size of string
        int[] arr = new int[string.length];
 
        // parsing the String argument as a signed decimal
        // integer object and storing that integer into the
        // array
        for (int i = 0; i < string.length; i++) {
            arr[i] = Integer.valueOf(string[i]);
        }
 
        // printing string
        System.out.print("String : " + str);
 
        // printing the Integer array
        System.out.print("\nInteger array : "
                         + Arrays.toString(arr));
    }
}


Output

String : [1,2,356,678,3378]
Integer array : [1, 2, 356, 678, 3378]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 2 – Using string.split() method

The string.split() method is used to split the string into various sub-strings. Then, those sub-strings are converted to an integer using the Integer.parseInt() method and store that value integer value to the Integer array.

Java




// Java Program to Convert String to Integer Array
// Using string.split() method
 
// Importing input output and utility classes
import java.io.*;
 
public class GFG {
    // Function for conversion
    static int[] method(String str)
    {
 
        String[] splitArray = str.split(" ");
        int[] array = new int[splitArray.length];
 
        // parsing the String argument as a signed decimal
        // integer object and storing that integer into the
        // array
        for (int i = 0; i < splitArray.length; i++) {
            array[i] = Integer.parseInt(splitArray[i]);
        }
        return array;
    }
 
    // main method
    public static void main(String[] args)
    {
        // Bufferedreader declaration
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));
 
        // string declaration
        String str = "1 23 456 7890";
        int i;
 
        // declaring the variable & calling the method with
        // passing string as an argument
        int[] array = method(str);
 
        // printing the string
        System.out.print("\nString : " + str);
 
        // printing the Integer array
        System.out.print("\nInteger array : [");
        for (i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
 
        System.out.print("]");
    }
}


Output

String : 1 23 456 7890
Integer array : [1 23 456 7890 ]

Time Complexity: O(n)
Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments