Scanner class (Java User Input)
For reading data from the console( for user input ), java scanner class can be used. It is found in java.util package.To use the scanner class, create an object of the class and use any of the available methods found in the scanner class documentation.
To read various types of data the following methods can be used
Example
import java.util.Scanner
class Test
{
public static void main(String[ ] args)
{ Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int num = sc.nextInt();
System.out.println("The entered number is" + num);
}
}
Output : Enter the number
5
The entered number is 5
Comments
Post a Comment