forked from LMU-CMSI186-03-SPRING2015/sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchDemo.java
More file actions
54 lines (53 loc) · 2.39 KB
/
Copy pathSwitchDemo.java
File metadata and controls
54 lines (53 loc) · 2.39 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class SwitchDemo {
public static void main(String[] args) {
try {
// Parse the first argument (args[0]) into an integer.
// If it can't, it will throw a NumberFormatException,
// which the catch statement will handle down below.
// If the length of args is 0, complain about the lack of args,
// handle that with an if statement.
if (args.length == 0) {
System.out.println("What are you high? You forgot an argument.");
} else {
int month = Integer.parseInt(args[0]);
String monthName = "";
// Sets the String monthName to a month depending on the
// value of the integer month, i.e., if month is 4, monthName
// becomes April.
switch (month) {
case 1: monthName = "January";
break;
case 2: monthName = "February";
break;
case 3: monthName = "March";
break;
case 4: monthName = "April";
break;
case 5: monthName = "May";
break;
case 6: monthName = "June";
break;
case 7: monthName = "July";
break;
case 8: monthName = "August";
break;
case 9: monthName = "September";
break;
case 10: monthName = "October";
break;
case 11: monthName = "November";
break;
case 12: monthName = "December";
// Message if the month is any other number than 1-12.
default: System.out.println("What are you high? That's not a month.");
break;
}
// Otherwise print out whatever name we got.
System.out.println(monthName);
}
} catch(NumberFormatException e) {
// More accusations of recreational drug use if the args[0] is not a number.
System.out.println("What are you high? That's not a number, bro.");
}
}
}