0

Why sc.nextLine(); is used in this code? (Java)

https://code.sololearn.com/ca21A17A10a1/?ref=app 1) What's the usage of sc.nextLine(); in this code? 2) Why the value of String is not printing when I remove sc.nextLine(); ? Please enlighten me 🙏

20th Dec 2020, 5:37 AM
Ray
Ray - avatar
7 Respostas
+ 3
Read this lesson and the associated comments : https://www.sololearn.com/learn/Java/2220/?ref=app
20th Dec 2020, 5:40 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
Rabeeh Your code has errors . You need to import the Scanner class like import java.util.Scanner; And there's no need for writing sc.nextLine() twice . String s = sc.nextLine(); is used to input or read a line of text ....
20th Dec 2020, 6:17 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
Rabeeh So i think your real Question on nextLine has been solved .Now about the new Question Ya idk as u said check the version .
20th Dec 2020, 6:27 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 2
Rabeeh Do you know how to assign value to a variable. If you know then this question should not come in your mind. You asked if we have String s = sc.nextLine() then why sc.nextLine() If you are cleared about concepts then you should know that sc.nextLine() returns String so here we assigned sc.nextLine() to a String variable s. String s = sc.nextLine(); Problem is that you have just completed the lessons but didn't understand the concepts.
20th Dec 2020, 8:01 AM
A͢J
A͢J - avatar
0
I already completed this lesson but my question is why the sc.nextLine(); here because we already have String s=sc.nextLine(); so what's the role of sc.nextLine(); ?
20th Dec 2020, 5:57 AM
Ray
Ray - avatar
0
Alphin K Sajan I see I didn't run the code in Sololearn and you're right when I import scanner it's working here but it's still showing error in IntelliJ ide. Is this because of Java version?
20th Dec 2020, 6:22 AM
Ray
Ray - avatar