Ajay Victor
Ajay Victor

Reputation: 153

Split Numbers within parenthesis in Java

I would like to split this input: 12132002(177) 012(207.5) 014(184) into two arrays, like this:
num[500] = 12132002,012,014, etc. and mark[500] = 177,207.5,184, etc.

The Fact is that I'm accepting values from user like this, where i don't know the total number which he/she will input.

How can I code in Java that kind of splitting? Is it like this?

int number[500];
for(int i=0;i<500;i++) {
    number=num.split("//(");
}

Upvotes: 2

Views: 209

Answers (3)

DigitShifter
DigitShifter

Reputation: 854

Try this:

public class Test {

    public static void main(String[] args) {
        // Guess this is a string since it is a mix of integers 
        // and non-integers, characters like '(', ')' and space. 
        String str = "12132002(177) 012(207.5) 014(184)"; 

        System.out.println("Your string:");
        System.out.println("str=\"" + str + "\"");
        System.out.println();

        // remove all ')' since they will not be used
        // using space as a delimiter is enough
        String str2 = str.replaceAll("\\)", "");

        System.out.println("Your string after removing ')' character:");
        System.out.println("str2=\"" + str2 + "\"");
        System.out.println();

        // Since your input has spaces, we split on spaces
        String[] strings = str2.split("\\s+");

        System.out.println("Result after splitting str2 by spaces:");
        for (String s : strings) {
            System.out.println(s);
        }
        System.out.println();

        // Lets make two array
        String[] num = new String[500];
        String[] mark= new String[500];

        // loop strings
        int cnt = 0;
        for (String s : strings) {
            String[] a = s.split("\\("); // a[0]="012", a[1]="207.5"
            num[cnt] = a[0];
            mark[cnt] = a[1];
            cnt++;
        }

        System.out.println("Result num: ");
        System.out.print("num[500] = ");
        for(String s : num) {
            if(s==null) {break;}
            System.out.print(s + ",");
        }
        System.out.println(" etc.\n");

        System.out.println("Result mark: ");
        System.out.print("mark[500] = ");
        for(String s : mark) {
            if(s==null) {break;}
            System.out.print(s + ",");
        }
        System.out.println(" etc.\n");
    }

}

Output:

Your string:
str="12132002(177) 012(207.5) 014(184)"

Your string after removing ')' character:
str2="12132002(177 012(207.5 014(184"

Result after splitting str2 by spaces:
12132002(177
012(207.5
014(184

Result num: 
num[500] = 12132002,012,014, etc.

Result mark: 
mark[500] = 177,207.5,184, etc.

Upvotes: 1

Brandzo
Brandzo

Reputation: 36

Ok buddy, this could be a solution for your problem. I chose to use the methods I have already created for some other project, but I think those can fit for this purpose as well, instead of using some complex REGEX expression. The output is good, though you have to figure out the way you want to store num and mark variables (I suggest arrays). Hope I helped.

public class Example {
    public static void main(String[] args) {
        String s = "12132002(177)012(207.5)014(184)";

        // output 12132002,012,014 && 177,207.5,184
        // it works good with this string as well -> s = "12132002(177)012(207.5)014(184)111(024)";
        int numOfParanthesis = numOfParanthesis(s, '(');
        String num = "";
        String mark = "";
        // array which contains positions of (
        int[] indexesOpening = indexes(s, '(');
        // array which contains positions of )
        int[] indexesClosing = indexes(s, ')');
        // logic
        for(int i = 0; i < numOfParanthesis; i++){
            if(i == 0){ 
                num = s.substring(i, indexesOpening[i])+",";
                mark = s.substring(indexesOpening[i]+1,indexesClosing[i])+",";

            }else if(i!=numOfParanthesis-1){
                num += s.substring(indexesClosing[i-1]+1, indexesOpening[i])+",";
                mark += s.substring(indexesOpening[i]+1, indexesClosing[i])+",";
            }else{
                num += s.substring(indexesClosing[i-1]+1, indexesOpening[i]);
                mark += s.substring(indexesOpening[i]+1, indexesClosing[i]);
            }
        }
        System.out.println(num);
        System.out.println(mark);
    }

    // returns array of positions for the given character
     public static int[] indexes(String s, char c){
        int numOfParanthesis = numOfParanthesis(s, c);
        int[] indexes = new int[numOfParanthesis];
        int delimetar = s.indexOf(c);
        for(int i = 0; i < numOfParanthesis; i++){
            if(i != -1){
                indexes[i] = delimetar;    
            }
            delimetar = s.indexOf(c, delimetar+1); 
        }
        return indexes;
    } 

    // returns how many times a character repeats in a string
    public static int numOfParanthesis(String s, char c){
        int number = s.indexOf(c);
        int i = 0;
        while (number >= 0){
            number = s.indexOf(c, number+1);
            i++;
        }
        return i;
    }
}

Upvotes: 1

MasterBlaster
MasterBlaster

Reputation: 968

To code "that kind of splitting", you will have to:

  1. Declare your variables: String[] number, String[] mark, String num, and String[] numSplit.
  2. Split num by " " (spaces). Assign this to numSplit.
  3. Loop through numSplit from 0 to numSplit.length with i.
  4. Set number[i] to numSplit[i] from the beginning to the first occurrence of a "(".
  5. Set mark[i] to numSplit[i] from one character after the first occurrence of "(" to one character before the end.
  6. Output number and mark

The full code:

String[] number = new String[500]; //1
String[] mark = new String[500];
String num = "12132002(177) 012(207.5) 014(184)";
String[] numSplit = num.split(" "); //2

for(int i = 0; i < numSplit.length; i++) { //3
    number[i] = numSplit[i].substring(0, numSplit[i].indexOf("(")); //4
    mark[i] = numSplit[i].substring(numSplit[i].indexOf("(") + 1, numSplit[i].length() - 1); //5
}

for(int i = 0; i < number.length; i++) System.out.println(number[i]); //6
for(int i = 0; i < mark.length; i++)  System.out.println(mark[i]);

Which outputs:

12132002
012
014
null (x497)
177
207.5
184
null (x497)

Notice that number, mark, and numSplit are String arrays because the leading zeros would be cut off in not otherwise. If you don't mind the leading zeros being cut off then you can change num to an int array and mark to a double array (Because of the decimal in 207.5).

Upvotes: 1

Related Questions