<double>, <double>
非常慢,所以最好使用Scanner.nextLine()
。 然后,您可以使用
BufferedReader
和
indexOf()
substring()
您要寻找的是拆分字符串
try (BufferedReader in = Files.newBufferedReader(filePath)) {
for (String line; (line = in.readLine()) != null; ) {
int idx = line.indexOf(", ");
if (idx == -1)
throw new IllegalArgumentException("Invalid line: " + line);
double d1, d2;
try {
d1 = Double.parseDouble(line.substring(0, idx));
d2 = Double.parseDouble(line.substring(idx + 2));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid line: " + line, e);
}
// code using d1 and d2 here
}
}
然后将结果解析为double
String string = "4.0,3.2";
String[] results = string.split(",");
String firstDouble = results[0];
String secondDouble = results[1];