Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- digit = ord(char) - ord('0')
- digit = ord(char) - ord('0')
- if digit > 9 then digit -= 7
- public static int parseInt(String s, int radix)
- throws NumberFormatException
- {
- if (s == null) {
- throw new NumberFormatException("null");
- }
- if (radix < Character.MIN_RADIX) {
- throw new NumberFormatException("radix " + radix +
- " less than Character.MIN_RADIX");
- }
- if (radix > Character.MAX_RADIX) {
- throw new NumberFormatException("radix " + radix +
- " greater than Character.MAX_RADIX");
- }
- int result = 0;
- boolean negative = false;
- int i = 0, max = s.length();
- int limit;
- int multmin;
- int digit;
- if (max > 0) {
- if (s.charAt(0) == '-') {
- negative = true;
- limit = Integer.MIN_VALUE;
- i++;
- } else {
- limit = -Integer.MAX_VALUE;
- }
- multmin = limit / radix;
- if (i < max) {
- digit = Character.digit(s.charAt(i++),radix);
- if (digit < 0) {
- throw NumberFormatException.forInputString(s);
- } else {
- result = -digit;
- }
- }
- while (i < max) {
- // Accumulating negatively avoids surprises near MAX_VALUE
- digit = Character.digit(s.charAt(i++),radix);
- if (digit < 0) {
- throw NumberFormatException.forInputString(s);
- }
- if (result < multmin) {
- throw NumberFormatException.forInputString(s);
- }
- result *= radix;
- if (result < limit + digit) {
- throw NumberFormatException.forInputString(s);
- }
- result -= digit;
- }
- } else {
- throw NumberFormatException.forInputString(s);
- }
- if (negative) {
- if (i > 1) {
- return result;
- } else { /* Only got "-" */
- throw NumberFormatException.forInputString(s);
- }
- } else {
- return -result;
- }
- }
- public class StringToInt {
- public int ConvertStringToInt(String s) throws NumberFormatException
- {
- int num =0;
- for(int i =0; i<s.length();i++)
- {
- if(((int)s.charAt(i)>=48)&&((int)s.charAt(i)<=59))
- {
- num = num*10+ ((int)s.charAt(i)-48);
- }
- else
- {
- throw new NumberFormatException();
- }
- }
- return num;
- }
- public static void main(String[]args)
- {
- StringToInt obj = new StringToInt();
- int i = obj.ConvertStringToInt("1234123");
- System.out.println(i);
- }
- }
- public static int parseInteger(String stringNumber) {
- int sum=0;
- int position=1;
- for (int i = stringNumber.length()-1; i >= 0 ; i--) {
- int number=stringNumber.charAt(i) - '0';
- sum+=number*position;
- position=position*10;
- }
- return sum;
- }
- int convertStringtoInt(String number){
- int total =0;
- double multiplier = Math.pow(10, number.length()-1);
- for(int i=0;i<number.length();i++){
- total = total + (int)multiplier*((int)number.charAt(i) -48);
- multiplier/=10;
- }
- return total;
- }
Add Comment
Please, Sign In to add comment