【问题标题】:Java - Scanning comma delimited double and int valuesJava - 扫描逗号分隔的 double 和 int 值
【发布时间】:2017-02-22 23:39:34
【问题描述】:

我正在尝试使用 Java 的 Scanner 类来扫描以逗号分隔的 double 和 int 值。

下面的Scanner input = new Scanner(System.in).useDelimiter("\\D");只能扫描,分隔的int值。例如输入 = 1000,2,3

如何扫描以, 分隔的 double 和 int 值,例如输入 = 1000.00,3.25,5100.00,2,3.5?

我尝试了以下方法,但它们似乎不起作用:

Scanner input = new Scanner(System.in).useDelimiter(",");
Scanner input = new Scanner(System.in).useDelimiter("\\,");
Scanner input = new Scanner(System.in).useDelimiter("[,]");

使用这些似乎会挂起代码。输入示例输入后,System.out.println 并未对扫描的 in 变量执行。

下面是我的示例代码:

import java.io.*;
import java.util.Scanner;

public class Solution {
  public static void main(String args[] ) throws Exception {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    System.out.print("Enter your values: ");
    // Scanner input = new Scanner(System.in).useDelimiter("\\D");
    Scanner input = new Scanner(System.in).useDelimiter(",");
    // Scanner input = new Scanner(System.in).useDelimiter("\\,");
    // Scanner input = new Scanner(System.in).useDelimiter("[,]");

    double investmentAmount = input.nextDouble();
    double monthlyInterestRate = input.nextDouble() / 100 / 12;
    double numberOfYears = input.nextDouble();
    double duration = numberOfYears * 12;

    double futureInvestmentValue = investmentAmount * Math.pow((1 + monthlyInterestRate), duration);
    System.out.println(investmentAmount);
    System.out.println(monthlyInterestRate);
    System.out.println(numberOfYears);
    System.out.println(duration);
    System.out.println("Accumulated value is " + futureInvestmentValue);
  }
}

找到解决方案

将 Scanner 行更新为以下内容似乎已修复它:

Scanner input = new Scanner(System.in).useDelimiter("[,\n]");

【问题讨论】:

  • 您的代码对我来说运行良好。你的输入输出是什么?
  • @4castle,我的输入是1000.00,3.25,1 都在一行上作为一个输入。输入输入后代码挂起,因此没有输出。我在 OpenJDK Java 7 上
  • @eduardo-dennis 如果代码有效,我会的。它仍然与 input.nextLine(); 挂起,就像您在最后一个 nextDouble() 之后对其进行编码并在一行上输入 1000.00,3.25,1 作为输入一样。
  • @j7an 复制并粘贴我的确切代码,它按预期工作。
  • @eduardo-dennis 我已经这样做了

标签: java regex java.util.scanner delimiter


【解决方案1】:

您很可能遇到Locale 问题,并且您的Scanner 尝试使用逗号分隔符解析双精度,但您将逗号设置为扫描仪分隔符。尝试以下解决方案:

Scanner input = new Scanner(System.in)
        .useDelimiter(",")
        .useLocale(Locale.ENGLISH);

这会将双打分隔符设置为点,并且您的逗号分隔双打应该可以正常工作。

确保将逗号放在输入末尾以解析最后一个值,例如1000.00,3.25,5,(甚至可能是您的输入不起作用的主要原因)

【讨论】:

  • 感谢您解决这个问题。输入以逗号结束。
【解决方案2】:

您面临的问题是因为 nextDouble() 不消耗最后一行。尝试在最后添加一个 input.nextLine() ,它应该可以按预期工作。

   /* Enter your code here. Read input from STDIN. Print output to STDOUT */
System.out.print("Enter your values: ");
Scanner input = new Scanner(System.in).useDelimiter(",");

double investmentAmount = input.nextDouble();
double monthlyInterestRate = input.nextDouble() / 100 / 12;
double numberOfYears = input.nextDouble();
input.nextLine();
double duration = numberOfYears * 12;

double futureInvestmentValue = investmentAmount * Math.pow((1 + monthlyInterestRate), duration);
System.out.println(investmentAmount);
System.out.println(monthlyInterestRate);
System.out.println(numberOfYears);
System.out.println(duration);
System.out.println("Accumulated value is " + futureInvestmentValue);

Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

【讨论】:

  • 是的,我必须接受所有在线输入。抱歉不清楚。
  • 我刚刚意识到您以逗号结束了您的输入。我试过了,它奏效了。如果我不以逗号结束输入。它不会起作用。
  • @j7an 检查我的编辑。你可以添加一个 input.nextLine() 来解决这个问题。
  • 我在最后一个 nextDouble() 下方添加了String x = input.nextLine();,但它仍然挂起。问题是 useDelimiter 需要分隔逗号和回车符“[,\n]”
  • 我添加了代码,它对我有用,不需要最后一个,.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 2015-08-31
相关资源
最近更新 更多