Guest User

Untitled

a guest
Nov 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. digit = ord(char) - ord('0')
  2.  
  3. digit = ord(char) - ord('0')
  4. if digit > 9 then digit -= 7
  5.  
  6. public static int parseInt(String s, int radix)
  7. throws NumberFormatException
  8. {
  9. if (s == null) {
  10. throw new NumberFormatException("null");
  11. }
  12.  
  13. if (radix < Character.MIN_RADIX) {
  14. throw new NumberFormatException("radix " + radix +
  15. " less than Character.MIN_RADIX");
  16. }
  17.  
  18. if (radix > Character.MAX_RADIX) {
  19. throw new NumberFormatException("radix " + radix +
  20. " greater than Character.MAX_RADIX");
  21. }
  22.  
  23. int result = 0;
  24. boolean negative = false;
  25. int i = 0, max = s.length();
  26. int limit;
  27. int multmin;
  28. int digit;
  29.  
  30. if (max > 0) {
  31. if (s.charAt(0) == '-') {
  32. negative = true;
  33. limit = Integer.MIN_VALUE;
  34. i++;
  35. } else {
  36. limit = -Integer.MAX_VALUE;
  37. }
  38. multmin = limit / radix;
  39. if (i < max) {
  40. digit = Character.digit(s.charAt(i++),radix);
  41. if (digit < 0) {
  42. throw NumberFormatException.forInputString(s);
  43. } else {
  44. result = -digit;
  45. }
  46. }
  47. while (i < max) {
  48. // Accumulating negatively avoids surprises near MAX_VALUE
  49. digit = Character.digit(s.charAt(i++),radix);
  50. if (digit < 0) {
  51. throw NumberFormatException.forInputString(s);
  52. }
  53. if (result < multmin) {
  54. throw NumberFormatException.forInputString(s);
  55. }
  56. result *= radix;
  57. if (result < limit + digit) {
  58. throw NumberFormatException.forInputString(s);
  59. }
  60. result -= digit;
  61. }
  62. } else {
  63. throw NumberFormatException.forInputString(s);
  64. }
  65. if (negative) {
  66. if (i > 1) {
  67. return result;
  68. } else { /* Only got "-" */
  69. throw NumberFormatException.forInputString(s);
  70. }
  71. } else {
  72. return -result;
  73. }
  74. }
  75.  
  76. public class StringToInt {
  77.  
  78. public int ConvertStringToInt(String s) throws NumberFormatException
  79. {
  80. int num =0;
  81. for(int i =0; i<s.length();i++)
  82. {
  83. if(((int)s.charAt(i)>=48)&&((int)s.charAt(i)<=59))
  84. {
  85. num = num*10+ ((int)s.charAt(i)-48);
  86. }
  87. else
  88. {
  89. throw new NumberFormatException();
  90. }
  91.  
  92. }
  93. return num;
  94. }
  95.  
  96. public static void main(String[]args)
  97. {
  98. StringToInt obj = new StringToInt();
  99. int i = obj.ConvertStringToInt("1234123");
  100. System.out.println(i);
  101. }
  102.  
  103. }
  104.  
  105. public static int parseInteger(String stringNumber) {
  106. int sum=0;
  107. int position=1;
  108. for (int i = stringNumber.length()-1; i >= 0 ; i--) {
  109. int number=stringNumber.charAt(i) - '0';
  110. sum+=number*position;
  111. position=position*10;
  112.  
  113. }
  114. return sum;
  115. }
  116.  
  117. int convertStringtoInt(String number){
  118.  
  119. int total =0;
  120. double multiplier = Math.pow(10, number.length()-1);
  121. for(int i=0;i<number.length();i++){
  122.  
  123. total = total + (int)multiplier*((int)number.charAt(i) -48);
  124. multiplier/=10;
  125.  
  126. }
  127.  
  128. return total;
  129. }
Add Comment
Please, Sign In to add comment