Java - 扫描逗号分隔的double和int值 [英] Java - Scanning comma delimited double and int values

查看:117
本文介绍了Java - 扫描逗号分隔的double和int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use Java's Scanner class to scan in double and int values delimited by commas.

以下扫描程序输入= new扫描程序(System.in).useDelimiter(\\\\); 只能扫描由分隔的int值。例如input = 1000,2,3

The following Scanner input = new Scanner(System.in).useDelimiter("\\D"); can only scan int values separated by ,. e.g. input = 1000,2,3

如何扫描由$ $ c $分隔的double和int值c>,例如input = 1000.00,3.25,5 100.00,2,3.5

How do I scan in double and int values separated by , e.g. input = 1000.00,3.25,5 or 100.00,2,3.5?

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

I tried the following but they don't seem to work:

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

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

Using these seems to hang the code. After entering the example input, System.out.println did not execute for the scanned in variables.

下面是我的示例代码:

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);
  }
}

找到解决方案

将扫描仪行更新为以下内容似乎修复了它:

Updating the Scanner line to the following seem to have fixed it:

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


推荐答案

很可能你有区域设置问题和您的扫描程序尝试使用逗号分隔符解析双打,但您将逗号设置为扫描程序分隔符。尝试以下解决方案:

Most likely you have Locale issues and your Scanner tries to parse doubles with comma delimeter, but you set comma as a scanner delimeter. Try the following solution:

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

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

This will set doubles delimiter to dot and your comma-separated doubles should work fine.

请务必将逗号放在输入的末尾以解析最后一个值,例如: 1000.00,3.25,5,(甚至可能是您输入无法正常工作的主要原因)

Be sure to place the comma at the end of input to parse the last value, e.g. 1000.00,3.25,5, (may be even it is the primary reason of your inputs not working)

这篇关于Java - 扫描逗号分隔的double和int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆