The code for getting user's input in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

The code for getting user's input in java?

Could you please give me an example of a valid java code which asks a user a question and allows him to write an answer? So far I have seen only one such code on SoloLearn, yet it doesn't ask a user anything, it just allows one to write input. I actually know how to do it with Scanner or BufferedReader, still, none of the codes which are running smoothly on Eclipse, do not run in the playground...

21st Sep 2018, 4:11 PM
ZdzichPych
ZdzichPych - avatar
2 Answers
+ 2
Thank you for your answer, I have already read this lesson, yet I still can't make my code first ask about a name and only then allow one to enter it. For instance, the code below asks about a name first, then I can insert it on Eclipse. Here on the playground it is performed simultaneously, so in practice, a user doesn't know what input he should insert without reading the code : import java.util.Scanner; class MyClass { public static void main(String[ ] args) { Scanner in = new Scanner(System.in); System.out.println("Enter your name: "); String name = in.nextLine(); System.out.println("Hello, " + name); } }
21st Sep 2018, 4:49 PM
ZdzichPych
ZdzichPych - avatar
+ 1
Thank you very much for your answer! As a Java beginner I was very much afraid of making mistakes and being not able to execute the code the way it should be executed. At least I know now that the code is ok :)
21st Sep 2018, 7:06 PM
ZdzichPych
ZdzichPych - avatar