Integer class. This may come in handle for parsing integers (for operations on an...
[IRC.git] / Robust / src / ClassLibrary / Integer.java
1 public class Integer {
2     private int value;
3
4     public Integer(int value) {
5         this.value=value;
6     }
7
8     public Integer(String str) {
9         value=Integer.parseInt(str, 10);
10     }
11
12     public int intValue() {
13         return value;
14     }
15
16     public static int parseInt(String str) {
17         return Integer.parseInt(str, 10);
18     }
19
20     public static int parseInt(String str, int radix) {
21         int value=0;
22         boolean isNeg=false;
23         int start=0;
24         byte[] chars=str.getBytes();
25         if (chars[0]=='-') {
26             isNeg=true;
27             start=1;
28         }
29         for(int i=start;i<str.length();i++) {
30             byte b=chars[i];
31             int val;
32             if (b>='0'&&b<='9')
33                 val=b-'0';
34             else if (b>='a'&&b<='z')
35                 val=10+b-'a';
36             else if (b>='A'&&b<='Z')
37                 val=10+b-'A';
38             if (val>=radix)
39                 System.error();
40             value=value*radix+val;
41         }
42         if (isNeg)
43             value=-value;
44         return value;
45     }
46
47     public String toString() {
48         return String.valueOf(value);
49     }
50
51     public int hashCode() {
52         return value;
53     }
54 }