-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInput_Using_Scanner.java
More file actions
40 lines (32 loc) · 1.38 KB
/
Input_Using_Scanner.java
File metadata and controls
40 lines (32 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Program: Input Using Scanner
* Description: A Java program to demonstrate how to accept various types of input from the user
* (String, int, float) using the Scanner class.
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/OOPM-JAVA-LAB
*/
import java.util.Scanner;
public class Input_Using_Scanner {
public static void main(String args[]) {
// Initializing Scanner object for keyboard input
Scanner scanner = new Scanner(System.in);
System.out.println("---------------------------------------------");
System.out.println(" Scanner Input Demo");
System.out.println("---------------------------------------------");
// Accepting String input
System.out.print("Enter a string: ");
String text = scanner.nextLine();
System.out.println("You entered string: " + text);
// Accepting Integer input
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
System.out.println("You entered integer: " + number);
// Accepting Float input
System.out.print("Enter a float value: ");
float decimal = scanner.nextFloat();
System.out.println("You entered float: " + decimal);
System.out.println("---------------------------------------------");
// Closing scanner
scanner.close();
}
}