在Java中的括号内拆分数字

时间:2016-06-30 17:18:01

标签: java string split numbers

我想将此输入:12132002(177) 012(207.5) 014(184)拆分为两个数组,如下所示:
num[500] = 12132002,012,014, etc.mark[500] = 177,207.5,184, etc.

事实是我接受了这样的用户价值,我不知道他/她将输入的总数。

我如何在Java中编码那种分裂?是这样的吗?

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

3 个答案:

答案 0 :(得分:1)

要编码“那种分裂”,您必须:

  1. 声明您的变量: IBM AA 2000-01-01 0.190049 0.200745 2000-01-02 -0.239746 -0.434157 2000-01-03 -0.112571 -0.302251 2000-01-04 -1.764957 -0.810951 2000-01-05 -0.961327 1.436247 IBM AA Z 2000-01-01 0.190049 0.200745 NaN 2000-01-02 -0.239746 -0.434157 NaN 2000-01-03 -0.112571 -0.302251 NaN 2000-01-04 -1.764957 -0.810951 NaN 2000-01-05 -0.961327 1.436247 NaN 2000-01-08 0.006128 NaN 0.383452 String[] numberString[] mark
  2. String num(空格)拆分String[] numSplit。将其分配给num
  3. 使用" "numSplitnumSplit循环0
  4. numSplit.length的开头到第一次出现,将i设为number[i]
  5. numSplit[i]第一次出现后的一个字符中将"("设置为mark[i]到结束前的一个字符。
  6. 输出numSplit[i]"("
  7. 完整代码:

    number

    哪个输出:

    mark

    请注意,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]); 12132002 012 014 null (x497) 177 207.5 184 null (x497) numbermark数组,因为前导零将被切断,否则将被切断。如果您不介意将前导零切断,则可以将numSplit更改为String数组,将num更改为int数组(因为{中的小数] {1}})。

答案 1 :(得分:1)

好的伙计,这可能是您问题的解决方案。我选择使用我已经为其他项目创建的方法,但我认为这些方法也适用于此目的,而不是使用一些复杂的REGEX表达式。输出很好,但你必须弄清楚你想要存储num和标记变量的方式(我建议数组)。希望我帮忙。

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;
    }
}

答案 2 :(得分:1)

试试这个:

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");
    }

}

<强>输出:

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.