Reading Input from console(Scanner class)

 You can use the Scanner class for console input. Java uses System.out to refer to the standard output device and System.in to the standard input device. By default the output device is the display monitor, and the input device is the keyboard. To perform console output, you simply use the println method to display a primitive value or a string to the console. Console input is not directly supported in Java, but you can use the Scanner class to create an object to read input from System.in, as follows:
Scanner input = new Scanner(System.in);
The syntax new Scanner(System.in) creates an object of the Scanner type. The syntax Scanner input declares that input is a variable whose type is Scanner. The whole line Scanner input = new Scanner(System.in) creates a Scanner object and assigns its reference to the variable input. An object may invoke its methods. To invoke a method on an object is to ask the object to perform a task.


Method                              Description
nextByte()                          reads an integer of the byte type.
nextShort()                         reads an integer of the short type.
nextInt()                             reads an integer of the int type.
nextLong()                         reads an integer of the long type.
nextFloat()                         reads a number of the float type.
nextDouble()                     reads a number of the double type. 
next()                                reads a string that ends before a white_space character.
nextLine()                         reads a line of text (i.e., a string ending with the Enter key pressed).

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons