-
Notifications
You must be signed in to change notification settings - Fork 1
/
Printer.java
47 lines (38 loc) · 1.39 KB
/
Printer.java
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
41
42
43
44
45
46
47
import java.util.Scanner;
class InputPrinter<T> {
private T value;
public void setValue(T value) {
this.value = value;
}
public void printTypeAndValue() {
if (value instanceof Integer) {
System.out.println("Type: Integer");
} else if (value instanceof String) {
System.out.println("Type: String");
} else {
System.out.println("Type: Unknown");
}
System.out.println("Value: " + value);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt(); // Number of test cases
for (int i = 0; i < T; i++) {
int c = scanner.nextInt(); // Indicator for type of input
if (c == 1) {
String str = scanner.next();
InputPrinter<String> inputPrinter = new InputPrinter<>();
inputPrinter.setValue(str);
inputPrinter.printTypeAndValue();
} else if (c == 2) {
int num = scanner.nextInt();
InputPrinter<Integer> inputPrinter = new InputPrinter<>();
inputPrinter.setValue(num);
inputPrinter.printTypeAndValue();
} else {
System.out.println("Invalid input");
}
}
scanner.close();
}
}