Java: Changing scanner.nextDouble accepting a dot as decimal-seperator OR writing doubles with a comma in a file

huangapple go评论109阅读模式
英文:

Java: Changing scanner.nextDouble accepting a dot as decimal-seperator OR writing doubles with a comma in a file

问题

  1. File myfile = new File("Example.txt");
  2. Scanner scanner1 = new Scanner(myFile);
  3. Double double1 = scanner1.nextDouble();
  4. try (Writer filewriter = new FileWriter(myFile)) {
  5. filewriter.write(double1.toString().replace(".", ",")); // Writing the double with a comma separator
  6. filewriter.flush();
  7. } catch (IOException e1) {
  8. System.err.println("oops");
  9. }
  10. Scanner scanner2 = new Scanner(myFile);
  11. String doubleStr = scanner2.next();
  12. Double double2 = Double.parseDouble(doubleStr.replace(",", ".")); // Reading the double with a dot separator
英文:
  1. File myfile = new File("Example.txt"); // the text of myFile looks like this: "3,0"
  2. Scanner scanner1 = new Scanner(myFile);
  3. Double double1 = scanner1.nextDouble(); // reading "3,0" - everything okay
  4. try (Writer filewriter = new FileWriter(myFile)) {
  5. filewriter.write(double1); // writing "3.0" - inconsistent but works for me
  6. filewriter.flush(); // (which obviously will not work for Java)
  7. } catch (IOException e1) {
  8. System.err.println("oops");
  9. }
  10. Scanner scanner2 = new Scanner(myFile);
  11. 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

  1. // 写入文件时,将数据值转换为使用逗号而不是小数点:
  2. filewriter.write(String.valueOf(double1).replace(".", ","));
  3. // 读取文件并转换数据值:
  4. // 创建一个用于Scanner读取的File对象。
  5. File myFile = new File("myfile.txt");
  6. // 确保文件存在。
  7. if (!myFile.exists()) {
  8. throw new IllegalArgumentException("无法找到以下文件路径!" +
  9. System.lineSeparator() + myFile.getAbsolutePath());
  10. }
  11. // 在此使用Try With Resources以自动关闭读取器。
  12. try (Scanner scanner2 = new Scanner(myFile)) {
  13. // 从文件中读取每个数值标记...
  14. while (scanner2.hasNext()) {
  15. // 声明一个初始化为null的Double变量
  16. Double double2 = null;
  17. /* 读取标记并删除任何数值块字符
  18. 例如,3.456,33中,点在欧洲国家常用作千位分隔符,
  19. 逗号作为小数点)。我们还将逗号小数分隔符
  20. 转换为点,以适应您的本地和双精度数据类型。 */
  21. String dblStrg = scanner2.next().replaceAll("[\\.\\s']", "").replaceAll(",", ".");
  22. /* 确保数值字符串值实际上是整数或双精度数据类型的字符串表示形式。 */
  23. if (dblStrg.matches("-?\\d+(\\.\\d+)?")) {
  24. /* 是的,将数值转换为双精度数据类型。 */
  25. double2 = Double.parseDouble(dblStrg);
  26. }
  27. /* 但是,如果从文件中读取的数值
  28. 不是有效的整数或双精度数,则通知用户。 */
  29. if (double2 == null) {
  30. System.out.println("无效的双精度数! (" + dblStrg + ")");
  31. }
  32. // 显示双精度类型变量的内容。
  33. else {
  34. System.out.println(double2);
  35. }
  36. }
  37. }
  38. catch (FileNotFoundException ex) {
  39. System.err.println(ex);
  40. }
英文:

To Write to the file, convert the data value to be written with a comma instead of a decimal point:

  1. filewriter.write(String.valueOf(double1).replace(".", ","));

To Read the file and convert the data values:

  1. // Create a File object to use in Scanner reader.
  2. File myFile = new File("myfile.txt");
  3. // Make sure file exists.
  4. if (!myFile.exists()) {
  5. throw new IllegalArgumentException("The following file path can not be found!" +
  6. System.lineSeparator() + myFile.getAbsolutePath());
  7. }
  8. // Try With Resourses used here to auto-close reader.
  9. try (Scanner scanner2 = new Scanner(myFile)) {
  10. // Read in each numerical token from file...
  11. while (scanner2.hasNext()) {
  12. // Declare a Double variable initialized to null
  13. Double double2 = null;
  14. /* Read in a token and remove any numerical block charaters
  15. for example, 3.456,33 the point is common in European
  16. countries as a thousands separator and the comma as a
  17. decimal point). We also convert the comma decimal separator
  18. to a dot to accomodate your local and the double data type. */
  19. String dblStrg = scanner2.next().replaceAll("[\\.\\s']", "").replaceAll(",", ".");
  20. /* Make sure the numerical String value is
  21. in fact a string representation of an
  22. Integer or a double data type. */
  23. if (dblStrg.matches("-?\\d+(\\.\\d+)?")) {
  24. /* It is so convert the numerical value
  25. to a double data type. */
  26. double2 = Double.parseDouble(dblStrg);
  27. }
  28. /* If however the numerical value read from file
  29. is not a valid Integer or double then inform
  30. User as such. */
  31. if (double2 == null) {
  32. System.out.println("Invalid Double Number! (" + dblStrg + ")");
  33. }
  34. // Display the contents of the double type variable.
  35. else {
  36. System.out.println(double2);
  37. }
  38. }
  39. }
  40. catch (FileNotFoundException ex) {
  41. System.err.println(ex);
  42. }

huangapple
  • 本文由 发表于 2020年9月8日 01:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63781599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定