Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

regex - 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.

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

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("[,]");

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

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("[,
]");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...