4

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("[,\n]");
j7an
  • 94
  • 2
  • 10
  • 1
    Your code is working fine for me. What's your input and output? – 4castle Oct 13 '16 at 18:09
  • @4castle, my input is `1000.00,3.25,1` all on one line as one input. There's no output as the code hangs after entering the input. I'm on OpenJDK Java 7 – j7an Oct 13 '16 at 18:27
  • @eduardo-dennis I would if the code works. It still hangs with `input.nextLine();` as how you coded it after the last nextDouble() and entering `1000.00,3.25,1` as the input all on one line. – j7an Oct 14 '16 at 18:47
  • @j7an copy and paste my exact code it works just as expected. – Eduardo Dennis Oct 14 '16 at 18:49
  • @eduardo-dennis I've done that already – j7an Oct 14 '16 at 18:51

2 Answers2

4

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)

Dmitry Smorzhok
  • 615
  • 9
  • 20
0

The issue you faced is because nextDouble() doesn't consume the last line. Try adding an input.nextLine() at the end it should work as expected.

   /* 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

Community
  • 1
  • 1
Eduardo Dennis
  • 12,511
  • 11
  • 72
  • 102
  • Yes, I must accept all the input on online. Sorry for not being clear. – j7an Oct 13 '16 at 18:33
  • I just realized you ended your input with a comma. I tried that and it worked. If I don't end the input with a comma. It won't work. – j7an Oct 13 '16 at 18:35
  • @j7an check my edit. You can add an input.nextLine() to fix that issue. – Eduardo Dennis Oct 13 '16 at 18:39
  • I've added `String x = input.nextLine();` below the last nextDouble() and it still hangs. The issue is that useDelimiter needs to delimit both the comma and the carriage return "[,\n]" – j7an Oct 13 '16 at 18:56
  • I added the code, it is working for me without the need of the last ,. – Eduardo Dennis Oct 13 '16 at 18:57