+ 1
what does Scanner(System.in) mean?
I know to create an instance of the scanner class we used scanner myvar= new ; scanner(system.in); but what is the use of the last line?
4 Answers
+ 17
Scanner is class in java which is used to get input from user.
+ 4
Scanner is a pre defined class in java available on java.util package....it is coded in such a way to get an input from user....
Scanner scan = new Scanner(System.in);
the above line creates an instance for object
scan of class Scanner....you can use scan object for getting user input....
+ 2
Here you have some errors.
Scanner myvar = new Scanner(System.in);
This is the correct line
Here you are creating an instance variable of Scanner class. By System.in you are saying that hey! I will take input from the console!
Hope you have got the point!
+ 2
System.in is variable with definition about what is standard input stream.
usualy it is console user keyboard input. Scanner is class for read this input stream.
import java.util.Scanner;
class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input numbers");
int n = 1, sum=0;
while( sc.hasNextInt() ) {
n = sc.nextInt(); //read int number from input
System.out.printf( "%d + %d = %d\n", sum, n, sum+=n);
}
}
}