[Java] I got an error when I used nextInt, nextLine and substring.

less than 1 minute read

import java.util.Scanner;

Cause is nextInt
If you type 123 in nextInt
It is converted to 123\n on the Java side (\n is entered at the end.)
nextInt returns only 123
Next, when you enter nextLine, the remaining \n will be read.
An error occurs.

The solution is
Describe the nextLine without assignment to variables. (Scanner.nextLine() in the source below)
By doing this, \n will be deleted and nextLine will work normally.

* NextInt does not have a function to erase \n, so it is necessary to insert nextLine.
* A backslash and n represent a line break.

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

         int a = 0;
         int b = 0;
         String str = "";

         a = scanner.nextInt();
         b = scanner.nextInt();
         str = scanner.nextLine();
         System.out.println(str.substring(a-1, b));