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.
Sorry I'm late by 2 months, if by for SOME REASON you are still a little confused (probably not, but regardless) the reason is as follows: Whenever you use scan.nextInt, scan.nextDouble etc, it does not pick up the newLine character (often represented in text as \n). So basically let's say within the buffer is "1.2341\n141\nSome Text\nSome More Text", we could do scan.nextDouble(); scan.nextInt();, and that would leave us in the buffer "\nSome Text\nSome More Text" whenever you use scan.nextLine(), it takes everything until it hits a newLine character, except here we're hitting a newLine IMMEDIATELY. This means that when we use scan.nextLine(), we get an empty String. To avoid this and get the Line we wanted, we run scan.nextLine() to remove that \n that is in the way so we get the next Line, which is "Some Text". Then we could use scan.nextLine() to get "Some More Text" since scan.nextLine() removes the new Line character it hit. Basically, the problem lies when we use scan.nextInt etc. It does not remove the new Line character, clogging the system. For that reason, I normally prefer to do scan.nextLine() always and just parsing, although by some that would be considered bad practice do to errors etc, but I feel like it is a better solution since you can see more directly where the error happened. Anywho, you do you and I do me. :3
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 →
Sorry I'm late by 2 months, if by for SOME REASON you are still a little confused (probably not, but regardless) the reason is as follows: Whenever you use scan.nextInt, scan.nextDouble etc, it does not pick up the newLine character (often represented in text as \n). So basically let's say within the buffer is "1.2341\n141\nSome Text\nSome More Text", we could do scan.nextDouble(); scan.nextInt();, and that would leave us in the buffer "\nSome Text\nSome More Text" whenever you use scan.nextLine(), it takes everything until it hits a newLine character, except here we're hitting a newLine IMMEDIATELY. This means that when we use scan.nextLine(), we get an empty String. To avoid this and get the Line we wanted, we run scan.nextLine() to remove that \n that is in the way so we get the next Line, which is "Some Text". Then we could use scan.nextLine() to get "Some More Text" since scan.nextLine() removes the new Line character it hit. Basically, the problem lies when we use scan.nextInt etc. It does not remove the new Line character, clogging the system. For that reason, I normally prefer to do scan.nextLine() always and just parsing, although by some that would be considered bad practice do to errors etc, but I feel like it is a better solution since you can see more directly where the error happened. Anywho, you do you and I do me. :3