Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Convert String to Integer Array

  • Last Updated : 10 Jun, 2021

Given a string, the task is to convert that string into an integer array. In order to simply let us first go through few illustrations to make it clear.

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 an in-build method in lang package. Integer.parseInt() method is used to parse the string argument to a signed decimal Integer object. The characters in the string must be integer values.  



Example

Java




// 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 an in-build method in lang package. Integer.valueOf() method is used to return the integer object. The characters in the string must be integer values.  

Example

Java




// 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 takes two arguments, one is 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.

Example 

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]

Method 4: Using string.split() method

string.split() method is used to split 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




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

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :