-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigit.java
More file actions
31 lines (27 loc) · 696 Bytes
/
Digit.java
File metadata and controls
31 lines (27 loc) · 696 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
27
28
29
30
31
class Digit extends Node {
private Digit(String value) {
super(value);
if (value.length() == 0) {
System.out.println("Tried to create empty digit, exiting");
System.exit(1);
}
}
public double calculate() {
return Double.parseDouble(this.value);
}
static Node parse(String s) {
String digit = "";
for (int i = 0; i < s.length(); i++) {
String curr = s.substring(i, i+1);
if (Operator.isOperator(curr)) break;
try {
Double.parseDouble(curr);
} catch (Exception e) {
System.out.printf("Invalid digit: '%s'", curr);
System.exit(1);
}
digit += curr;
}
return new Digit(digit);
}
}