You are viewing a single comment's thread. Return to all comments →
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
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:
int i2 = scan.nextInt();
double d2 = scan.nextDouble();
String s2 = scan.nextLine();
System.out.println( i + i2);
System.out.println(d + d2);
System.out.println(s + s2);
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 ....".