TechieClues TechieClues
Updated date Aug 13, 2024
This blog explores the process of converting a string of numbers into an integer array in Java.

Method 1: Using StringTokenizer and parseInt

This method involves using the StringTokenizer class to split the input string into individual tokens and then convert each token into an integer using the Integer.parseInt() method. 

import java.util.StringTokenizer;

public class StringToIntArrayConverter {
    public static int[] convert(String str) {
        StringTokenizer tokenizer = new StringTokenizer(str);
        int[] arr = new int[tokenizer.countTokens()];

        int index = 0;
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            arr[index] = Integer.parseInt(token);
            index++;
        }

        return arr;
    }

    public static void main(String[] args) {
        String input = "10 20 30 40 50";
        int[] result = convert(input);

        System.out.println("Output:");
        for (int num : result) {
            System.out.print(num + " ");
        }
    }
}

Output:

10 20 30 40 50

The StringTokenizer class is used to split the input string into individual tokens. We then create an integer array of the same size as the number of tokens. In the while loop, we iterate through each token, convert it to an integer using Integer.parseInt(), and store it in the array. Finally, we return the resulting integer array.

Method 2: Using String.split and Integer.valueOf

The second method uses the split() method of the String class to split the input string into an array of strings. Then, each string element is converted to an integer using the Integer.valueOf() method. 

public class StringToIntArrayConverter {
    public static int[] convert(String str) {
        String[] strArray = str.split(" ");
        int[] arr = new int[strArray.length];

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

        return arr;
    }

    // Main method and output remain the same as in Method 1
}

Output:

10 20 30 40 50

In this approach, we split the input string into an array of strings using the split() method and provide the delimiter as a space. Then, we create an integer array of the same size as the string array. Using a simple for loop, we iterate through each string element, and convert it to an integer using Integer.valueOf(), and store it in the integer array.

Method 3: Using Java 8 Streams

This method takes advantage of Java 8 Stream API to achieve the string-to-integer array conversion in a more concise manner. 

import java.util.Arrays;
import java.util.stream.IntStream;

public class StringToIntArrayConverter {
    public static int[] convert(String str) {
        int[] arr = Arrays.stream(str.split(" "))
                          .mapToInt(Integer::parseInt)
                          .toArray();

        return arr;
    }

    // Main method and output remain the same as in Method 1
}

Output:

10 20 30 40 50

With Java 8 Streams, we can chain multiple operations together in a concise and expressive manner. In this approach, we split the input string using split(), create a stream of strings, and map each string to its corresponding integer value using mapToInt(), and finally convert the stream to an integer array using toArray().

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!