+ 2

How can we set a method which asks for an input in java

please create a program which asks input from the user and try to explain the program to me in description Thanks

9th Apr 2018, 12:13 PM
Devansh Gupta
Devansh Gupta - avatar
2 Answers
+ 5
/*Importing class Scanner from java.util package*/ import java.util.Scanner; /*main method*/ /*Creating a new object of class Scanner and passing an argument (System.in) so it knows were to look for inputstream i.g. Keyboard*/ Scanner scn = new Scanner(System.in); /*Assigning scanner to a string type to read input until the end of the inputstream, so basicly whatever you input will read by the scanner and assigned to variable str until the end of the line if you start a new line your input wont be reconised as you havent gave the program instructions to do so and your new line will be ignored*/ String str = scn.nextLine();
9th Apr 2018, 12:58 PM
D_Stark
D_Stark - avatar
+ 4
I made this it might help you to understand how scanner works without using System.in for input. https://code.sololearn.com/c1GVLeFOROre/?ref=app
9th Apr 2018, 2:17 PM
D_Stark
D_Stark - avatar