Benefits of learning parseint () for integer classes-3

Source: Internet
Author: User
  • 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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.