Skip to content
Advertisement

How do I split the plus minus number string in java? [closed]

Input :

String arrr[] = new String[4];

arrr[0] = +2501 +2502 +2503 +2504

arrr[1] = -2501 -2504 +2505 +2506 +2507 +2509

arrr[2] = +2501 +2511 -2502 -2505

arrr[3] = +2513 -2507 -2503 -2511 -2509

Output :

I want separte the string as :

Positive :

arrr1[0] = +2501 +2502 +2503 +2504

arrr1[1] = +2505 +2506 +2507 +2509

arrr1[2] = +2501 +2511

arrr1[3] = +2513

Negative :

arrr2[0] = -2501 -2504

arrr2[1] = -2502 -2505

arrr2[2] = -2507 -2503 -2511 -2509

int nostrt = -1, m = 0;
StringBuilder str4 = new StringBuilder();
for(int i = 0;i < strbullist.length(); i++)
{    
    char c = strbullist.charAt(i);

    if(nostrt == -1)
    {
        m = i;
        nostrt = 1; 
    }
    if(c=='-')
    {
        
        str4.append(strbullist.substring(m, i));
        nostrt = -1;
        System.out.println(str4);
    }    
}

Advertisement

Answer

You can split the string on one or more whitespace characters (i.e. s+) and iterate the resulting array to find if an element starts with a positive symbol or a negative symbol.

Demo:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
        List<String> positiveNums = new ArrayList<>();
        List<String> negativeNums = new ArrayList<>();
        String[] arr = str.split("\s+");

        for (String s : arr) {
            if (s.startsWith("+")) {
                positiveNums.add(s);
            } else if (s.startsWith("-")) {
                negativeNums.add(s);
            }
        }

        System.out.println("Positive numbers: " + positiveNums);
        System.out.println("Negative numbers: " + negativeNums);

        // If you want to store the output into string variables
        String positiveValues = positiveNums.toString();
        String negativeValues = negativeNums.toString();
        System.out.println("Positive numbers: " + positiveValues);
        System.out.println("Negative numbers: " + negativeValues);
    }
}

Output:

Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]
Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]

Alternatively, you can also use regex, +d+|-d+ which means one or more digits followed by a plus symbol (i.e. +d+) or (i.e. |) one or more digits followed by a negative symbol (i.e. -d+).

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
        Matcher matcher = Pattern.compile("\+\d+|\-\d+").matcher(str);
        List<String> positiveNums = new ArrayList<>();
        List<String> negativeNums = new ArrayList<>();

        while (matcher.find()) {
            String s = matcher.group();
            if (s.startsWith("+")) {
                positiveNums.add(s);
            } else if (s.startsWith("-")) {
                negativeNums.add(s);
            }
        }

        System.out.println("Positive numbers: " + positiveNums);
        System.out.println("Negative numbers: " + negativeNums);
    }
}

Output:

Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]

In case you want to use StringBuilder instead of the List:

public class Main {
    public static void main(String[] args) {
        String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
        StringBuilder positiveNums = new StringBuilder();
        StringBuilder negativeNums = new StringBuilder();
        String[] arr = str.split("\s+");

        for (String s : arr) {
            if (s.startsWith("+")) {
                positiveNums.append(s + " ");
            } else if (s.startsWith("-")) {
                negativeNums.append(s + " ");
            }
        }

        String positiveValues = positiveNums.toString().trim();
        String negativeValues = negativeNums.toString().trim();
        System.out.println("Positive numbers: " + positiveValues);
        System.out.println("Negative numbers: " + negativeValues);
    }
}

Output:

Positive numbers: +2501 +2502 +2503 +2504 +2505 +2506 +2507 +2509 +2501 +2511 +2513
Negative numbers: -2501 -2504 -2502 -2505 -2507 -2503 -2511 -2509

Using String#matches:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
        List<String> positiveNums = new ArrayList<>();
        List<String> negativeNums = new ArrayList<>();
        String[] arr = str.split("\s+");

        for (String s : arr) {
            if (s.matches("\+\d+")) {
                positiveNums.add(s);
            } else if (s.matches("\-\d+")) {
                negativeNums.add(s);
            }
        }

        System.out.println("Positive numbers: " + positiveNums);
        System.out.println("Negative numbers: " + negativeNums);
    }
}

Output:

Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]

Update (based on the updated question):

If the strings in your question are elements of a String[], you need an additional loop to iterate this String[]. Rest of the things will remain the same.

public class Main {
    public static void main(String... args) {
        String[] strArr = { "+2501 +2502 +2503 +2504", "-2501 -2504 +2505 +2506 +2507 +2509", "+2501 +2511 -2502 -2505",
                "+2513 -2507 -2503 -2511 -2509" };
        StringBuilder positiveNums = new StringBuilder();
        StringBuilder negativeNums = new StringBuilder();

        for (String str : strArr) {
            String[] arr = str.split("\s+");
            for (String s : arr) {
                if (s.startsWith("+")) {
                    positiveNums.append(s + " ");
                } else if (s.startsWith("-")) {
                    negativeNums.append(s + " ");
                }
            }
        }

        String positiveValues = positiveNums.toString().trim();
        String negativeValues = negativeNums.toString().trim();
        System.out.println("Positive numbers: " + positiveValues);
        System.out.println("Negative numbers: " + negativeValues);
    }
}

Output:

Positive numbers: +2501 +2502 +2503 +2504 +2505 +2506 +2507 +2509 +2501 +2511 +2513
Negative numbers: -2501 -2504 -2502 -2505 -2507 -2503 -2511 -2509
Advertisement