+ 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
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();
+ 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