0

我有以下输入 (11,C) (5,) (7,AB) 我需要将它们分成每个坐标的 2 部分。所以我的 intarray 应该有 11, 5, 7 而我的字母数组应该有 C,,AB

但是当我尝试使用 stringtokenizer 时,我只得到我的 intarray 应该有 11、5、7 而我的字母数组应该有 C、AB

有什么办法可以得到(5,)的空白部分?谢谢你。

Vector<String> points = new Vector<String> ();
String a = "(11,C) (5,) (7,AB)";
StringTokenizer st = new StringTokenizer(a, "(,)");
    while(st.hasMoreTokens()) {
        points.add(st.nextToken());
    }
}
System.out.println(points);
4

4 回答 4

2
List <Integer> digits = new ArrayList <Integer> ();
List <String> letters = new ArrayList <String> ();
Matcher m = Pattern.compile ("\\((\\d+),(\\w*)\\)").matcher (string);
while (m.find ())
{
    digits.add (Integer.valueOf (m.group (1)));
    letters.add (m.group (2));
}
于 2013-02-06T10:39:42.000 回答
0
public static void main(String[] args) {
    String s = "(11,C), (5,) ,(7,AB)";
    ArrayList<String> name = new ArrayList<String>();
    ArrayList<Integer> number = new ArrayList<Integer>();

    int intIndex = 0, stringIndex = 0;
    String[] arr = s.split(",");
    for (int i = 0; i < arr.length; i++) {
        String ss = arr[i].replace("(", "");
        ss = ss.replace(")", "");
        boolean b = isNumeric(ss);
        // System.out.println( Arrays.toString(arr));
        if (b) {
            int num = Integer.valueOf(ss.trim()).intValue();
            number.add(num);
        } else
            name.add(ss);
    }

    System.out.println(name);
    System.out.println(number);

}



public static boolean isNumeric(String str) {
    try {
        double d = Double.parseDouble(str);
    } catch (NumberFormatException nfe) {
        return false;
    }
    return true;
}

试试这个:我将输入从 "(11,C) (5,) (7,AB)" 稍微更改为 "(11,C), (5,) ,​​(7,AB)" 。

输出:

[C,  , AB]
[11, 5, 7]
于 2013-02-06T10:53:22.490 回答
0

原始级别的残酷编码:

List<String> points = new ArrayList<String> ();
    String source= "(11,C) (5,) (7,AB)";
    StringTokenizer deleteLeft = new StringTokenizer(source, "(");
        while(deleteLeft.hasMoreTokens()) {
            StringTokenizer deleteRight = new StringTokenizer(deleteLeft.nextToken(), ")");
                points.add(deleteRight.nextToken());
        }
        System.out.println(points);
    }
于 2013-02-06T10:56:34.623 回答
0

应该是这样的

String[] values = a.split("\\) \\(");
String[][] result = new String[values.length][2];
for (int i = 0; i < values.length; i++) {
    values[i] = values[i].replaceAll("\\(|\\)", "") + " ";
    result[i] = values[i].split("\\,");  
    System.out.println(result[i][0] + " * " + result[i][1]);
}

结果将包含坐标对。

于 2013-02-06T10:36:23.123 回答