How to convert string to Integer array in java

Given a string, the task is to convert the string into an array of integers. For the sake of simplicity, let us first make it clear through a few pictures.

Illustrations:

Input  : string : "1 2 3 4 5 6 7 8 9 0"
Output : Integer array : [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Input  : string : {"123", "345", "437", "894"}
Output : Integer array : [123, 345, 437, 894]
Input  : string : "[1,2,356,678,3378]"
Output : Integer array : [1, 2, 356, 678, 3378]

Methods:

  1. Using Integer.parseInt() method
  2. Using Integer.valueOf() method
  3. Using string.replaceAll() method
  4. Using string.split() method

Method 1: Using Integer.parseInt() method

This is a built-in method in the lang package. The Integer.parseInt() method is used to parse string parameters into signed decimal integer objects. The characters in the string must be integer values.

Example

 
// Java Program to Convert String to Integer Array
// Using

// Importing input output classes
import java.io.*;
import java.util.Arrays;

// Main class
public class GFG
{
// Main driver method
public static void main(String args[])
{
	// Declaring a string	
	String [] str = {"123", "345", "437", "894"};
	int size = str.length;
	
	//array declaration with string size
	int [] arr = new int [size];
	
	//printing the string
	System.out.println("String : " + str);
	
	//parsing the String argument as a signed decimal integer object and
	//storing that integer into the array
	for(int i=0; i<size; i++)
	{
		arr[i] = Integer.parseInt(str[i]);
	}
	
	//printing the integer array
	System.out.println("Integer array : " + Arrays.toString(arr));
}
}

Output

String : [Ljava.lang.String;@2f4d3709
Integer array : [123, 345, 437, 894]

Method 2: Using Integer.valueOf() method

This is a built-in method in the lang package. The Integer.valueOf() method is used to return an integer object. The characters in the string must be integer values.

Example

 
// Java Program to Convert String to Integer Array
// Using Integer.valueOf() method

// Importing input output and utility classes
import java.io.*;
import java.util.Arrays;

// Main class
public class GFG {

// main driver method
public static void main(String[] args) {
	//string declaration
	String [] str = {"123", "345", "437", "894"};
	int size = str.length;
	
	//declaring an array with the size of string
	int [] arr = new int [size];
	
	//printing the string
	System.out.print("String : " +str);
		
	//parsing the String argument as a signed decimal integer object and
	//storing that integer into the array
	for(int i=0; i<size; i++)
	{
		arr[i] = Integer.valueOf(str[i]);
	}
	
	//printing the Integer array
	System.out.println("Integer array : " + Arrays.toString(arr));
}
}

Output

String : [Ljava.lang.String;@2f4d3709Integer array : [123, 345, 437, 894]

Method 3: Using string.replaceAll() method  

The replaceAll() method of the String class has two parameters, one is the regular expression and the replacement value. This method will replace the given regular expression with the given replacement value, and then split the string using the split() method.

Example 

 
// 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]

Method 4: Using string.split() method

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

Example

 
// Java Program to Convert String to Integer Array
// // Java Program to Convert String to Integer Array
// Using Integer.valueOf() method

// Importing input output and utility classes

// Importing input output and utility classes
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GFG
{
	// Function for conversion
	static int[] method(String str)
	{
		int i;
	
		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(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 ]

 

Submit Your Programming Assignment Details