fixed the broken RegEx. (It wouldn't read negative numbers)

This commit is contained in:
Morlok8k 2012-08-19 02:11:47 -07:00
parent 267474b20c
commit aa77cde46d

View File

@ -98,14 +98,28 @@ public class Coordinates {
* @author jaseg * @author jaseg
*/ */
public static Coordinates parseStringRegEx(String stringOfCoords) { public static Coordinates parseStringRegEx(String stringOfCoords) {
Matcher shortForm = Pattern.compile("\\((\\d+),(\\d+)\\)").matcher(stringOfCoords); int X = 0, Y = 0, Z = 0;
if (shortForm.matches()) { return new Coordinates(Integer.parseInt(shortForm.group(1)), 64, boolean matched = false;
Integer.parseInt(shortForm.group(2))); } Matcher shortForm = Pattern.compile("\\((-?\\d+),(-?\\d+)\\)").matcher(stringOfCoords);
Matcher normalForm = Pattern.compile("\\[(\\d+),(\\d+),(\\d+)\\]").matcher(stringOfCoords); if (shortForm.matches()) {
if (normalForm.matches()) { return new Coordinates(Integer.parseInt(shortForm.group(1)), X = Integer.parseInt(shortForm.group(1));
Integer.parseInt(shortForm.group(2)), Integer.parseInt(shortForm.group(3))); } Y = 64;
Main.err("Invalid coordinate format: " + stringOfCoords); Z = Integer.parseInt(shortForm.group(2));
return new Coordinates(0, 0, 0); matched = true;
}
Matcher normalForm =
Pattern.compile("\\[(-?\\d+),(-?\\d+),(-?\\d+)\\]").matcher(stringOfCoords);
if (normalForm.matches()) {
X = Integer.parseInt(normalForm.group(1));
Y = Integer.parseInt(normalForm.group(2));
Z = Integer.parseInt(normalForm.group(3));
matched = true;
}
if (!matched) {
System.err.println("Invalid coordinate format: " + stringOfCoords);
System.err.println();
}
return new Coordinates(X, Y, Z);
} }
/** /**