-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanMethod.java
More file actions
26 lines (21 loc) · 841 Bytes
/
ScanMethod.java
File metadata and controls
26 lines (21 loc) · 841 Bytes
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
import java.util.Scanner;
public class ScanMethod{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Enter your age:");
int age = scanner.nextInt();
scanner.nextLine();
/*When use nextInt(); it will only scan integers. so "\n" part will remain in the
* scanner .To awoid that we again call the nextLine(); after that scanner will be empty
* and we can use it as usuall
*/
System.out.println("Enter your favorite sport:");
String sport = scanner.nextLine();
scanner.close(); // Best parctice,Optional
System.out.println("Your name is "+name);
System.out.println("Your age is "+age);
System.out.println("Your favorite sport is "+sport);
}
}