我有以下输入(11,C) (5,) (7,AB)我需要为每个坐标将它们分成两部分。所以我的intarray应该有11,5,7,字母数组应该有C,,AB
但是当我尝试使用字符串标记器时,我得到的只是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);发布于 2013-02-06 18:39:42
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-06 18:36:23
一定是这样的
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-06 18:53:22
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]https://stackoverflow.com/questions/14726474
复制相似问题