A is for Ambition A is for Ambition - 1 year ago 89
Java Question

How to get Java scanner to acknowledge blank input?

I am having trouble getting my program to respond to empty inputs. For example, say I wanted to prompt the user to enter a value for money with type

as well as a currency type. Here is what the program in question looks like.

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter the amount of money "
+ "and specify currency (USD or CNY): ");

// initialize variable moneyInput
BigDecimal moneyInput;

// check if moneyInput is numeric
try {
moneyInput = input.nextBigDecimal();
} catch (InputMismatchException e){
System.err.println("InputMismatchException: non-numeric value");

// specify currency of moneyInput
String currencyType = input.next();


So here, an input like
100.00 USD
, works fine.

Enter the amount of money and specify currency (USD or CNY): 100.00 USD
$100.00 USD ≈ ¥670.17 CNY

An input like
appropriately results in the error message.

Enter the amount of money and specify currency (USD or CNY): ASDF USD
InputMismatchException: non-numeric value

But, how do I force the program to respond to blank inputs, either immediately pressing the return key or entering a bunch of spaces in the first line? For example:

Enter the amount of money and specify currency (USD or CNY):

1000.00 USD
$1000.00 USD ≈ ¥6701.70 CNY

In the above example, the user can just press the return key indefinitely until something readable (valid or invalid) is entered. I want to implement some way to check if the user pressed the return key without entering anything meaningful.

Another outcome that I don't know how to account for is if the user enters ONLY the monetary value, then presses [return] a bunch of times before finally inputting the currency.


Enter the amount of money and specify currency (USD or CNY): 100.00

$100.00 USD ≈ ¥670.17 CNY

How do I get the program to prompt the user with something like
Please specify the currency:
after a numeric value is input and the user presses [return]? Something like

Enter the amount of money and specify currency (USD or CNY): 100.00
Please specify the currency: USD
$100.00 USD ≈ ¥670.17 CNY

I was able to achieve the above functionality by changing the part after the try-catch block to:

System.out.print("Please specify the currency: ");
String currencyType = input.nextLine();

But using this method, the program loses the ability to allow the user to input both
in one line, like in the first example. (And again, there is still the problem of being able to press [return] indefinitely for each of these prompts until something readable is finally input).

Thanks for reading and sorry for the long post.

Answer Source

Scanner offers two, sometimes conflicting, use cases:

  1. Read input by tokens, disregarding any whitespace inbetween (this is what your initial code does)
  2. Read input by line, without treating any contents of those lines specially

Generally speaking mixing the two is a poor idea (it works, but probably not how you intend).

If you want to handle Enter you need to read a user's input line-by-line with Scanner.nextLine() and parse the line manually (i.e. line.split("\\s+")), rather than use Scanner's token-reading methods.

Some people like to use nested Scanners and read input line by line with one Scanner then pass the line into a new Scanner to tokenize just that line.

For example:

try (Scanner in = new Scanner(System.in)) {
  while (in.hasNextLine()) {
    try {
      String line = in.nextLine();
      Scanner lineScan = new Scanner(line);
      BigDecimal moneyInput = lineScan.nextBigDecimal();
      String currency = lineScan.next();
      // do something
    } catch (NoSuchElementException | IllegalStateException e) {
      System.err.print("Please enter the VALUE followed by the CURRENCY: ");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download