Google , Val() ; ?
, Integer.parseInt(myString) Double.parseDouble(myString) Java. ; , , .
: :
public static double val(String str) {
StringBuilder validStr = new StringBuilder();
boolean seenDot = false;
boolean seenDigit = false;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '.' && !seenDot) {
seenDot = true;
validStr.append(c);
} else if ((c == '-' || c == '+') && !seenDigit) {
validStr.append(c);
} else if (Character.isDigit(c)) {
seenDigit = true;
validStr.append(c);
} else if (Character.isWhitespace(c)) {
continue;
} else {
break;
}
}
return Double.parseDouble(validStr.toString());
}
:
public static void main(String[] args) {
System.out.println(val(" 1615 198th Street N.E."));
System.out.println(val("2457"));
System.out.println(val(" 2 45 7"));
System.out.println(val("24 and 57"));
}
:
1615198.0
2457.0
2457.0
24.0
, , , Double.parseDouble - . , , , , . .