Tag Archives: console

Easy reading from Keyboard in Java

We can use Scanner class

Import first:

import java.util.Scanner;

Then we go this way for integers.

Scanner keyboard = new Scanner(System.in);
System.out.println("enter an integer");
int myint = keyboard.nextInt();

If we rather a string line:

String text= scan.nextLine();

Easy piece!

Note: If you are using nextInt() with nextLine() it may probably could have some trouble cause nextInt() does not read the last newline character of input and so nextLine().