+ 2
I am unable to understand that what is .nextLine() in this code ? Please explain.
import java.util.Scanner; class MyClass { public static void main(String[ ] args) { Scanner myVar = new Scanner(System.in); System.out.println(myVar.nextLine()); } }
1 Answer
+ 1
You see, myVar is an object of type Scanner, which is typically used in Java to get input from external means---namely, the user.
One of the methods included in Scanner is nextLine(), which allows the program to stop execution and wait for the user to type a line of characters: any number of characters followed by a RETURN/ENTER.
Your program receives the line and immediately displays it on screen using the usual System.Out.PrintLn.