As is specified in the Java Standard Editions docs, any number of underscore characters (_) can appear anywhere between digits in a numerical literal, like so:- int number = 100_000;.
Out of curiosity, I tried inputting an underscore separated value (100_000) to an input that is expecting an integer. The code:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int basicAmount = sc.nextInt();
}
}
This resulted in a generic error message.
So my questions are:
- Why does this happen? Why am I not allowed to input numeric values containing underscores?
- Is there a way to allow users to input numeric values with underscores?