+ 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...
2 ответов
+ 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);
}
}
+ 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 :)