英文:
Java: Changing scanner.nextDouble accepting a dot as decimal-seperator OR writing doubles with a comma in a file
问题
File myfile = new File("Example.txt");
Scanner scanner1 = new Scanner(myFile);
Double double1 = scanner1.nextDouble();
try (Writer filewriter = new FileWriter(myFile)) {
filewriter.write(double1.toString().replace(".", ",")); // Writing the double with a comma separator
filewriter.flush();
} catch (IOException e1) {
System.err.println("oops");
}
Scanner scanner2 = new Scanner(myFile);
String doubleStr = scanner2.next();
Double double2 = Double.parseDouble(doubleStr.replace(",", ".")); // Reading the double with a dot separator
英文:
File myfile = new File("Example.txt"); // the text of myFile looks like this: "3,0"
Scanner scanner1 = new Scanner(myFile);
Double double1 = scanner1.nextDouble(); // reading "3,0" - everything okay
try (Writer filewriter = new FileWriter(myFile)) {
filewriter.write(double1); // writing "3.0" - inconsistent but works for me
filewriter.flush(); // (which obviously will not work for Java)
} catch (IOException e1) {
System.err.println("oops");
}
Scanner scanner2 = new Scanner(myFile);
Double double2 = scanner2.nextDouble(); // There it is: "java.util.InputMismatchException"
My question is, how to make it write doubles with a seperation-comma in a file OR how to make scanner read doubles with a separation-dot. Both would be okay.
I already tried to use objects of DecimalFormat and so on, but it didn't change anything for me. That's why I would be very happy about some answers... Thank you for everybody trying.
答案1
得分: 1
// 写入文件时,将数据值转换为使用逗号而不是小数点:
filewriter.write(String.valueOf(double1).replace(".", ","));
// 读取文件并转换数据值:
// 创建一个用于Scanner读取的File对象。
File myFile = new File("myfile.txt");
// 确保文件存在。
if (!myFile.exists()) {
throw new IllegalArgumentException("无法找到以下文件路径!" +
System.lineSeparator() + myFile.getAbsolutePath());
}
// 在此使用Try With Resources以自动关闭读取器。
try (Scanner scanner2 = new Scanner(myFile)) {
// 从文件中读取每个数值标记...
while (scanner2.hasNext()) {
// 声明一个初始化为null的Double变量
Double double2 = null;
/* 读取标记并删除任何数值块字符
例如,3.456,33中,点在欧洲国家常用作千位分隔符,
逗号作为小数点)。我们还将逗号小数分隔符
转换为点,以适应您的本地和双精度数据类型。 */
String dblStrg = scanner2.next().replaceAll("[\\.\\s']", "").replaceAll(",", ".");
/* 确保数值字符串值实际上是整数或双精度数据类型的字符串表示形式。 */
if (dblStrg.matches("-?\\d+(\\.\\d+)?")) {
/* 是的,将数值转换为双精度数据类型。 */
double2 = Double.parseDouble(dblStrg);
}
/* 但是,如果从文件中读取的数值
不是有效的整数或双精度数,则通知用户。 */
if (double2 == null) {
System.out.println("无效的双精度数! (" + dblStrg + ")");
}
// 显示双精度类型变量的内容。
else {
System.out.println(double2);
}
}
}
catch (FileNotFoundException ex) {
System.err.println(ex);
}
英文:
To Write to the file, convert the data value to be written with a comma instead of a decimal point:
filewriter.write(String.valueOf(double1).replace(".", ","));
To Read the file and convert the data values:
// Create a File object to use in Scanner reader.
File myFile = new File("myfile.txt");
// Make sure file exists.
if (!myFile.exists()) {
throw new IllegalArgumentException("The following file path can not be found!" +
System.lineSeparator() + myFile.getAbsolutePath());
}
// Try With Resourses used here to auto-close reader.
try (Scanner scanner2 = new Scanner(myFile)) {
// Read in each numerical token from file...
while (scanner2.hasNext()) {
// Declare a Double variable initialized to null
Double double2 = null;
/* Read in a token and remove any numerical block charaters
for example, 3.456,33 the point is common in European
countries as a thousands separator and the comma as a
decimal point). We also convert the comma decimal separator
to a dot to accomodate your local and the double data type. */
String dblStrg = scanner2.next().replaceAll("[\\.\\s']", "").replaceAll(",", ".");
/* Make sure the numerical String value is
in fact a string representation of an
Integer or a double data type. */
if (dblStrg.matches("-?\\d+(\\.\\d+)?")) {
/* It is so convert the numerical value
to a double data type. */
double2 = Double.parseDouble(dblStrg);
}
/* If however the numerical value read from file
is not a valid Integer or double then inform
User as such. */
if (double2 == null) {
System.out.println("Invalid Double Number! (" + dblStrg + ")");
}
// Display the contents of the double type variable.
else {
System.out.println(double2);
}
}
}
catch (FileNotFoundException ex) {
System.err.println(ex);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论