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.
String nextLine(): Returns the next line of text, or, if you are in the middle of a line, returns the remainder of the line. Caution: If you are in the middle of a line, nextLine does not return the next line, but instead the remainder of the current line.
I guess this is why we have to add a
scan.nextLine();
before declaring the second String variable.
I would like to know if there is a better solution to this problem.
P.s.: For those of you (like me) who try to learn Java and solve the challenges via the video tutorial given here, it doesn't closely follow the challenges. So be sure to check the instructions given instead of trying to implement what you have learnt from the video.
P.p.s: I am new to coding, and I find the instructions and the written tutorial quite confusing and hard to understand. They sound like they have been written for those who are already familiar with coding. In the Day 1 challenge, it says:
/* Declare second integer, double, and String variables. *//* Read and save an integer, double, and String to your variables.*/
Although I completed this challenge successfully- at least it says so-, I don't know why "declare" and "read and save" are written separately while you can do those things on the same line . I guess, "declaring" should be assigning a value to a variable via "=". Then "read and save" should be the "scan.nextLine()" part. The instructions would be more useful if these stuff were explained a little bit better. As in " 'scan.next.Line' method is used to 'read and save' the variables and you can 'declare, read and save' a variable using only one line like ....".
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 1: Data Types
You are viewing a single comment's thread. Return to all comments →
In Java:
I guess this is why we have to add a
before declaring the second String variable.
I would like to know if there is a better solution to this problem.
This is how I solved this challenge:
P.s.: For those of you (like me) who try to learn Java and solve the challenges via the video tutorial given here, it doesn't closely follow the challenges. So be sure to check the instructions given instead of trying to implement what you have learnt from the video. P.p.s: I am new to coding, and I find the instructions and the written tutorial quite confusing and hard to understand. They sound like they have been written for those who are already familiar with coding. In the Day 1 challenge, it says:
Although I completed this challenge successfully- at least it says so-, I don't know why "declare" and "read and save" are written separately while you can do those things on the same line . I guess, "declaring" should be assigning a value to a variable via "=". Then "read and save" should be the "scan.nextLine()" part. The instructions would be more useful if these stuff were explained a little bit better. As in " 'scan.next.Line' method is used to 'read and save' the variables and you can 'declare, read and save' a variable using only one line like ....".