I got an error when using nextInt, nextLine and substring.

import java.util.Scanner;

/*
The cause is nextInt
If you enter 123 in nextInt
123 on the Java side\Converted to n (at the end\n is entered. )
nextInt returns only 123
Next, when you enter nextLine, it remains\n is read
An error occurs.

The solution is
Write nextLine without assignment to a variable. (Scanner from the following sources.nextLine())
By doing this\n is erased and nextLine works fine

* NextInt\Since there is no function to erase n, it is necessary to insert nextLine.
* Backslash and n represent line breaks.

*/
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();
        scanner.nextLine();
        str = scanner.nextLine();
        System.out.println(str.substring(a - 1, b));
    }
 }

Recommended Posts

I got an error when using nextInt, nextLine and substring.
I got an error when I ran rake routes.
When I bcrypt with node + docker, I got an error
I got an InvalidUseOfMatchersException when using any with JUnit Mock
I got an error and couldn't push! !! [error: failed to push some refs to]
When I ran a jar created using WildFly Swarm, I got an InvocationTargetException.
I get an error when adding a dependency
[Mockito] 3.2.4 → 3.3.x, but I got an error (Iketenai)
When using a list in Java, java.awt.List comes out and an error occurs
When registering a new user, I got an error called ActiveRecord :: NotNullViolation and how to deal with it.
Error when using SnapKit
I got an error "undefined method` create'" in RSpec
I got an error when trying to install sbt to build a Scala development environment
I got an error! * There is no interactive request template
[Docker] When I did docker-compose build, I got an error saying Can not read file. .. [Rails 6.0]
Error when using rails capybara
I got an IllegalAccessError when trying to use PowerMock with JUnit
Solution notes when an error occurs when downloading docker gpg using curl
I got a Permission Denied error when I put Laravel in Docker
I get an error with bundle install and puma cannot be installed.
I summarized the points to note when using resources and resources in combination
[Visual Studio Code] I get a syntax error when debugging when using rbenv
What to do and how to install when an error occurs in DXRuby 1.4.7
I want to display an error message when registering in the database
I can deploy Heroku, but I get an error and can't open it
When I tried to start GlassFish, I got an internal error while "Publishing to GlassFish 4.0 on localhost ...". What to do when it becomes
In WSL2, when I did `docker-compose up`, I got an error saying that the sh file was not found.
I built a CentOS 8 environment with Vagrant and tried to sync_folder, but I got an error, so I solved it.
I got an error when composer install (PHP Fatal error: Class UpdateHelper \ ComposerPlugin contains 2 \ methods and must therefore be declared abstract or implement the remaining methods)
A reminder when an aapt.exe error occurs
When I renew the certificate with CircleCI × fastlane, I get an exit status: 65 error.
I get an error when I try to use "^" or "$" in ruby ​​regular expression
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
Summary when trying to use Solr in Java and getting an error (Solr 6.x)