简体   繁体   English

Java - 扫描逗号分隔的double和int值

[英]Java - Scanning comma delimited double and int values

I'm trying to use Java's Scanner class to scan in double and int values delimited by commas. 我正在尝试使用Java的Scanner类来扫描用逗号分隔的double和int值。

The following Scanner input = new Scanner(System.in).useDelimiter("\\\\D"); 以下Scanner input = new Scanner(System.in).useDelimiter("\\\\D"); can only scan int values separated by , . 只能扫描相隔INT的值, eg input = 1000,2,3 例如输入= 1000,2,3

How do I scan in double and int values separated by , eg input = 1000.00,3.25,5 or 100.00,2,3.5 ? 如何扫描由...分隔的double和int值,例如input = 1000.00,3.25,5100.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("[,]");

Using these seems to hang the code. 使用这些似乎挂起代码。 After entering the example input, System.out.println did not execute for the scanned in variables. 输入示例输入后,System.out.println未针对已扫描的变量执行。

Below is my sample code: 以下是我的示例代码:

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

Solution found 找到解决方案

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. 您很可能遇到Locale问题,并且您的Scanner尝试使用逗号分隔符解析双打,但您将逗号设置为扫描仪分隔符。 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. 这会将双打分隔符设置为点,并且逗号分隔的双精度数应该可以正常工作。

Be sure to place the comma at the end of input to parse the last value, eg 1000.00,3.25,5, (may be even it is the primary reason of your inputs not working) 请务必将逗号放在输入的末尾以解析最后一个值,例如1000.00,3.25,5, (甚至可能是您输入无法工作的主要原因)

The issue you faced is because nextDouble() doesn't consume the last line. 您遇到的问题是因为nextDouble()不消耗最后一行。 Try adding an input.nextLine() at the end it should work as expected. 尝试添加一个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 在使用next(),nextInt()或其他nextFoo()方法后,Scanner正在跳过nextLine()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM