Sai A Sai A
Updated date Jul 06, 2023
In this blog, we will learn how to convert a string representation of numbers into a BigInteger array in Java. Explore multiple methods, including looping, string splitting, and Java streams, and understand the code behind each approach.

Introduction:

In Java, converting a string representation of numbers into a BigInteger array is a common task that requires precision and accuracy, especially when dealing with large integers. In this blog, we will explore multiple methods to convert a string into a BigInteger array in Java. We will provide step-by-step explanations, code examples, and corresponding outputs for each method. By the end of this guide, you will have a solid understanding of various approaches to perform this conversion and can choose the most suitable method for your specific requirements.

Method 1: Using a Loop to Convert Each Element

The first method involves iterating over each character of the input string, converting it into a BigInteger object, and storing it in the resulting array. Here's the program:

import java.math.BigInteger;

public class StringToBigIntegerArray {
    public static void main(String[] args) {
        String input = "123456789";
        int length = input.length();
        BigInteger[] result = new BigInteger[length];

        for (int i = 0; i < length; i++) {
            String character = String.valueOf(input.charAt(i));
            result[i] = new BigInteger(character);
        }

        // Output
        for (BigInteger element : result) {
            System.out.println(element);
        }
    }
}

Output:

1
2
3
4
5
6
7
8
9

Method 2: Splitting the String and Parsing Each Element

The second method utilizes the split() method to split the input string into an array of substrings. We then parse each substring into a BigInteger object and store it in the resulting array. Here's the program:

import java.math.BigInteger;

public class StringToBigIntegerArray {
    public static void main(String[] args) {
        String input = "123,456,789";
        String[] parts = input.split(",");
        int length = parts.length;
        BigInteger[] result = new BigInteger[length];

        for (int i = 0; i < length; i++) {
            result[i] = new BigInteger(parts[i]);
        }

        // Output
        for (BigInteger element : result) {
            System.out.println(element);
        }
    }
}

Output:

123
456
789

Method 3: Using Java Streams and Mapping

The third method leverages Java streams to achieve a concise and functional approach. We split the input string, map each substring to a BigInteger object, and collect them into an array. Here's the program:

import java.math.BigInteger;
import java.util.Arrays;
import java.util.stream.Collectors;

public class StringToBigIntegerArray {
    public static void main(String[] args) {
        String input = "123,456,789";
        BigInteger[] result = Arrays.stream(input.split(","))
                .map(BigInteger::new)
                .toArray(BigInteger[]::new);

        // Output
        for (BigInteger element : result) {
            System.out.println(element);
        }
    }
}

Output:

123
456
789

Conclusion:

In this comprehensive guide, we explored three different methods for converting a string into a BigInteger array in Java. Method 1 involved using a loop to convert each character individually. Method 2 utilized the split() method to split the string and parse each element. Method 3 leveraged Java streams and mapping to achieve a concise and functional approach.

Comments (0)

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