We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Unlike the scanner.nextLine() method, the scanner.nextInt() method only consumes the integer part and leaves the enter or newline character in the input buffer.
When the third scanner.nextLine() is called, it finds the enter or newline character still existing in the input buffer, mistakes it as the input from the user, and returns immediately.
There are two ways to solve this problem. You can either consume the newline character after the scanner.nextInt() call takes place, or you can take all the inputs as strings and parse them to the correct data type later on.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Stdin and Stdout II
You are viewing a single comment's thread. Return to all comments →
Unlike the scanner.nextLine() method, the scanner.nextInt() method only consumes the integer part and leaves the enter or newline character in the input buffer.
When the third scanner.nextLine() is called, it finds the enter or newline character still existing in the input buffer, mistakes it as the input from the user, and returns immediately.
There are two ways to solve this problem. You can either consume the newline character after the scanner.nextInt() call takes place, or you can take all the inputs as strings and parse them to the correct data type later on.