public static int stringToInteger(String str) {
if (str == null || str == "") {
throw new RuntimeException("not a valid number");
}
byte[] bytes = str.getBytes();
int digits = bytes.length;
int sum = 0;
for (byte b : bytes) {
if (b < 48 || b > 57) {
throw new RuntimeException("not a valid number");
}
int dueNum = mapStrNumByteToNum.get((b+""));
sum += dueNum * Math.pow(10, --digits);
}
return sum;
}
public static Map<String, Integer> mapStrNumByteToNum = new HashMap<String, Integer>();
static {
for (int i = 0; i < 10; i++) {
mapStrNumByteToNum.put((48 + i)+"", i);
}
}