- Usage of parseint () method --- strength of Exception Handling
Public static int parseint (string S, int Radix) <br/> <MCE: script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"> </MCE: SCRIPT> <MCE: script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"> </MCE: SCRIPT> throws numberformatexception <br/> {<br/> If (S = NULL) {<br/> throw new numberformatexception ("null "); <br/>}< br/> If (Radix <character. min_radix) {<br/> throw new numberformatexception ("Radix" + Radix + <br/> "less than character. min_radix "); <br/>}< br/> If (Radix> character. max_radix) {<br/> throw new numberformatexception ("Radix" + Radix + <br/> "greater than character. max_radix "); <br/>}< br/> int result = 0; <br/> Boolean negative = false; <br/> int I = 0, max = S. length (); <br/> int limit; <br/> int multmin; <br/> int digit; <br/> If (max> 0) {<br/> If (S. charat (0) = '-') {<br/> negative = true; <br/> Limit = integer. min_value; <br/> I ++; <br/>}else {<br/> Limit =-integer. max_value; <br/>}< br/> multmin = Limit/Radix; // eg: Radix = 10 <br/> if (I <max) {// note that s eg: S = "123" <br/> digit = character exists. digit (S. charat (I ++), Radix); // digit = 1 <br/> If (digit <0) {<br/> throw numberformatexception. forinputstring (s); <br/>} else {<br/> result =-digit; // result =-1; <br/>}< br/> while (I <max) {<br/> // accumulating negatively avoids surprises near max_value <br/> digit = character. digit (S. charat (I ++), Radix); // obtain the next character <br/> If (digit <0) {<br/> throw numberformatexception. forinputstring (s); <br/>}< br/> If (result <multmin) {// determine that when result <multimin should be followed by result = Result * Radix, for example, if you pass S = 21474836471 more than one digit, <br/> // The result will be less than <multmin <br/> throw numberformatexception. forinputstring (s); <br/>}< br/> result * = Radix; // <br/> If (result <Limit + digit) {// 2147483649 when the number of digits does not exceed, determine whether the last digit exceeds <br/> throw numberformatexception. forinputstring (s); <br/>}< br/> result-= digit; // For example, transfer-21 to-20-1 =-21 and add it directly <br/>}< br/>} else {<br/> throw numberformatexception. forinputstring (s); <br/>}< br/> If (negative) {<br/> if (I> 1) {<br/> return result; // return a negative number <br/>}else {/* only got "-" */<br/> throw numberformatexception. forinputstring (s); <br/>}< br/>} else {<br/> return-result; // return a positive number <br/>}< br/>}
Result: Record Return Value
Negative: Symbol
I: String position
S: String Length
Limit: Limit
Multmin: it is also a boundary.
Digit: Number of the Current Character
Digit = character. digit (S. charat (I ++), Radix); involved in Character Learning, next time to study.