我正在解析十进制格式的网格坐标。我想将模式匹配存储在 StringBuilder 数组中以供进一步操作。但是,在每个 while 循环中调用 append() 时,我得到一个空指针。
String coordinateExample = "N14585928W084144340";
this.parseCoordinate(coordinateExample);
...
public void parseCoordinate(String coordinateString) {
int i = 0;
//extract NSEW leading characters
Pattern pL = Pattern.compile("[A-Z]");
Matcher m = pL.matcher(coordinateString);
StringBuilder[] hemisphere = new StringBuilder[]{};
while (m.find()) {
hemisphere[i].append(m.group());
//System.out.println("m.group(): " + m.group());
i++;
}
// reset i
i = 0;
//extract decimal degree digits
Pattern pN = Pattern.compile("[0-9]++");
Matcher n = pN.matcher(coordinateString);
StringBuilder[] coordinate = new StringBuilder[]{};
while (n.find()) {
coordinate[i].append(n.group());
//System.out.println("q.group(): " + n.group());
i++;
}
}