1

I have the String data="5999999-log(1000)*100/1000-cbrt(log(10000^2))".I want to split the data String into int list Int[] numbers = {5999999,1000,100,1000,10000,2}so I can add commas separator to ever number in int list like 5,999,999 to every number in int list and I don't want the string like String[] s = {"-log(",")*","/","-cbrt(log(","^","))"} and the data string is editable where user can write the expression to solve so I can add commas in runtime to every numbergroup. is it possible? where I see in google calculator

  • Yes, it's possible, you can iterate the chars in the string and just once you find a number, you raise a flag indicating you are now parsing a number. Once you either reach the end of the string or a non-numerical character you have read the whole number and that value can be stored in an array / list / variable or anything. You continue this until you reach the end of the string. – Dan Baruch Nov 05 '20 at 08:41

1 Answers1

0

You can use the regex, \\d+ (which means a string of one or more digits) to get the matching strings from your given string and convert each of these matching strings into int using Integer#parseInt.

Demo:

import java.util.Arrays;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String data = "5999999-log(1000)*100/1000-cbrt(log(10000^2))";
        int[] numbers = Pattern.compile("\\d+")      // Pattern to match a string of one or more digits
                        .matcher(data)
                        .results()
                        .map(MatchResult::group)     // Map the MatchResult into String
                        .mapToInt(Integer::parseInt) // Map the String into int
                        .toArray();
        System.out.println(Arrays.toString(numbers));
    }
}

Output:

[5999999, 1000, 100, 1000, 10000, 2]

If you want to format each int into a String as per some number format, you can map the boxed IntStream (i.e. Stream<Integer>) into Stream<String> by applying the format.

Demo:

import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Locale;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String data = "5999999-log(1000)*100/1000-cbrt(log(10000^2))";
        final NumberFormat formatter = NumberFormat.getNumberInstance(Locale.US);
        String[] formattedNumbers = Pattern.compile("\\d+") // Pattern to match a string of one or more digits
                .matcher(data)
                .results()
                .map(MatchResult::group)                    // Map the each MatchResult into a String
                .mapToInt(Integer::parseInt)                // Map each String into an int
                .boxed()
                .map(number -> formatter.format(number))    // Format each int into a String using the formatter
                .toArray(String[]::new);                    // Convert Stream into array
        System.out.println(Arrays.toString(formattedNumbers));
    }
}

Output:

[5,999,999, 1,000, 100, 1,000, 10,000, 2]
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • @AyaanDeveloper - If you add commas in the numbers, they won't remain numbers; rather, they will become strings. Would you like to have `String []` in addition to this `int[]`? – Arvind Kumar Avinash Nov 05 '20 at 09:44