This tutorial is how you can read inputs for command prompt. You will be using Scanner class to read the input from keyboard.
You import Scanner class using > import java.util.Scanner ; statement
You create scanner object using the statement ;
Scanner myscan = new Scanner(System.in);
myscan can be any name.
To read integer number , you use the following statement.
x = myscan.nextInt();
The full code the program is stated below;
// How to read input from console
import java.util.Scanner;
public class ScannerInput2 {
public static void main(String[] args){
int x ;
Scanner myscan = new Scanner(System.in);
// Ask input
System.out.print("Enter an integer number :");
// Read from console
x = myscan.nextInt();
//Print out the number
System.out.println("Your integer number is :" + x );
}
}
No comments:
Post a Comment