I use a Scanner
to get inputs from the console in my Java project with this simple code:
char com = ' ';
Scanner sc = new Scanner(System.in);
com = sc.nextLine().charAt(0);
When I enter my expected commands (a,b,c,d...), it works fine, but if I push the Enter button of my keyboard without command entered, I have this error message and the app crash:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
How do I fix this issue?