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.
How this actually work is when we use sc.nextInt() or sc.nextDouble(), the system reads the integer tokens(which can be considered as a single word) and the input buffer is at the end of that integer(token). So basically, when we use sc.nextLine() the buffer reads only the remaining part until the next line (which is empty string). Therefore, by using sc.next() first, we directly go to new line and then we can concate (add) that upper word by using sc.nextLine() on the new line to get the whole string.
Example:
String s = sc.next();
s += sc.nextLine();
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 →
How this actually work is when we use sc.nextInt() or sc.nextDouble(), the system reads the integer tokens(which can be considered as a single word) and the input buffer is at the end of that integer(token). So basically, when we use sc.nextLine() the buffer reads only the remaining part until the next line (which is empty string). Therefore, by using sc.next() first, we directly go to new line and then we can concate (add) that upper word by using sc.nextLine() on the new line to get the whole string.
Example:
String s = sc.next();
s += sc.nextLine();