不用API就直接字符串解析不就行了吗,怎么就不可能了 String s = "12345"; int result = 0;for(int i = 0; i < s.length(); i++){ result = result + i*Math.pow(10,i); }System.out.println(result);
很简单呀 public static void main(String[] args) { int i = parseInt("356"); System.out.println(i); } public static int parseInt(String s) { if (s == null || s.equals("")) throw new IllegalArgumentException("参数不能为null或空串!"); int result = 0; for (int i = 0, len = s.length(); i < len; i++) { char c = s.charAt(i); if (c >= '0' && c <= '9') { result = result * 10 + (int) (c - '0'); } else { throw new IllegalArgumentException("s中只能包含数字"); } } return result; }
public class ConvertStringToInt { public static void main(String[] args) { String s = "1232"; int result = 0; int value; int index = 0; if(s.charAt(0) == '-') { index = 1; } for(; index<s.length(); index++) { value = s.charAt(index) - 48; if(value<0 || value>9) { throw new NumberFormatException(); } else { result = result * 10 + value; } } if(s.charAt(0) == '-') { result = 0 - result; } System.out.println(result); } } 不知道这样符不符合你的要求,我会继续想想有没有更好的办法的
0435 public static int parseInt(String s, int radix) 0436 throws NumberFormatException { 0437 if (s == null) { 0438 throw new NumberFormatException("null"); 0439 } 0440 0441 if (radix < Character.MIN_RADIX) { 0442 throw new NumberFormatException("radix " + radix 0443 + " less than Character.MIN_RADIX"); 0444 } 0445 0446 if (radix > Character.MAX_RADIX) { 0447 throw new NumberFormatException("radix " + radix 0448 + " greater than Character.MAX_RADIX"); 0449 } 0450 0451 int result = 0; 0452 boolean negative = false; 0453 int i = 0, len = s.length(); 0454 int limit = -Integer.MAX_VALUE; 0455 int multmin; 0456 int digit; 0457 0458 if (len > 0) { 0459 char firstChar = s.charAt(0); 0460 if (firstChar < '0') { // Possible leading "+" or "-" 0461 if (firstChar == '-') { 0462 negative = true; 0463 limit = Integer.MIN_VALUE; 0464 } else if (firstChar != '+') 0465 throw NumberFormatException.forInputString(s); 0466 0467 if (len == 1) // Cannot have lone "+" or "-" 0468 throw NumberFormatException.forInputString(s); 0469 i++; 0470 } 0471 multmin = limit / radix; 0472 while (i < len) { 0473 // Accumulating negatively avoids surprises near MAX_VALUE 0474 digit = Character.digit(s.charAt(i++), radix); 0475 if (digit < 0) { 0476 throw NumberFormatException.forInputString(s); 0477 } 0478 if (result < multmin) { 0479 throw NumberFormatException.forInputString(s); 0480 } 0481 result *= radix; 0482 if (result < limit + digit) { 0483 throw NumberFormatException.forInputString(s); 0484 } 0485 result -= digit; 0486 } 0487 } else { 0488 throw NumberFormatException.forInputString(s); 0489 } 0490 return negative ? result : -result; 0491 }这个是JDK的源代码
public class ConvertStringToInt { public static void main(String[] args) { String s = "-1233444"; int result = ConvertStringToInt.myPraseInt(s); System.out.println(result); }
public static int myPraseInt(String s) { if(s == null) {//如果为null直接抛出异常 throw new NumberFormatException("null"); } int result = 0;//记录最后返回的int值 int value;//字符串中每个字符转char换成int保存在value中 int index = 0;//遍历s的每个字符 int max = s.length();//s的长度 //int limit;//允许的范围;暂时还不清楚 boolean negative = false;//是否为负数,默认为false if(s.charAt(0) == '-') { negative = true; index ++; } while(index < max) { value = s.charAt(index ++) - 48; if(value<0 || value>9) { throw new NumberFormatException("" + s.charAt(index)); } else { result = result * 10 + value; } } if(negative) { if(index > 1) { return -result; } else {//只有一个负号的情况 throw new NumberFormatException(s); } } else { return result; } } } 重新考了了下,这个比较完善了。我也是去看源代码来完善的,于是出现了新的问题: Integer有两个静态方法能够将String转换成int值: public static int parseInt(String s); public static int parseInt(String s, int radix); 说明:其中带有两个参数的方法第二个参数指的是转换成的进制,而只带一个参数的 方法则表示默认转换成十进制,因此我们不难想到带有一个参数的方法最终也应该是调用带有两个参数的parseInt方法,现在潜入JDK源代码中验证一下: public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); } 而带有两个参数的parseInt方法源代码如下: public static int parseInt(String s, int radix) throws NumberFormatException { if (s == null) {//如果字符串为空就直接抛出异常 throw new NumberFormatException("null"); } if (radix < Character.MIN_RADIX) {//MIN_RADIX=2 throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); }//jdk规定了进制的范围是2~36的范围 if (radix > Character.MAX_RADIX) {//MAX_RADIX=36 throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } int result = 0;//存储最后返回的int值 boolean negative = false;//是否为负数,默认为false int i = 0, max = s.length();//用i遍历s,max存储s的长度 int limit;//存储允许的int的范围,如:为负数,那么limit=Integer.MIN_VALUE 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; //这里可能比较多人奇怪:怎么是负数,返回result?其实这是因为前面已经做了处理了,//这里的result已经是负数了。这主要是因为Integer的范围最小负数的绝对值比最大的正//数要大(大1)的原因 } else { /* Only got "-" */ throw NumberFormatException.forInputString(s); } } else { return -result; } } 然后那个multmin作用是什么??? 其实就是我的程序尚未实现的问题:怎么判断result是不是超出Integer范围了?? 很明显,直接判断result<Integer.MAX_VALUE是不行的,因为这样永远是true的吧,所以JDK也做了处理,而我就是处理这块不太懂,希望牛人可以帮忙解答一下。 也在线等等吧。 当然还有个更复杂的问题,就是JDK后台是怎么求字符(注意是字符ch)对应的int值的??应该不会像我们那样-48吧。
你这句话本身就用API中的函数了。int abc = new Integer(str); //需要JDK 1.5
Integer.parseInt("123456")
好无聊啊- -! 你叫他们不用API来开发个软件吧
String s = "12345";
int result = 0;for(int i = 0; i < s.length(); i++){
result = result + i*Math.pow(10,i);
}System.out.println(result);
int i = parseInt("356");
System.out.println(i);
} public static int parseInt(String s) {
if (s == null || s.equals(""))
throw new IllegalArgumentException("参数不能为null或空串!"); int result = 0;
for (int i = 0, len = s.length(); i < len; i++) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
result = result * 10 + (int) (c - '0');
} else {
throw new IllegalArgumentException("s中只能包含数字");
}
} return result;
}
public class ConvertStringToInt { public static void main(String[] args) {
String s = "1232";
int result = 0;
int value;
int index = 0;
if(s.charAt(0) == '-') {
index = 1;
}
for(; index<s.length(); index++) {
value = s.charAt(index) - 48;
if(value<0 || value>9) {
throw new NumberFormatException();
} else {
result = result * 10 + value;
}
}
if(s.charAt(0) == '-') {
result = 0 - result;
}
System.out.println(result);
}
}
不知道这样符不符合你的要求,我会继续想想有没有更好的办法的
0436 throws NumberFormatException {
0437 if (s == null) {
0438 throw new NumberFormatException("null");
0439 }
0440
0441 if (radix < Character.MIN_RADIX) {
0442 throw new NumberFormatException("radix " + radix
0443 + " less than Character.MIN_RADIX");
0444 }
0445
0446 if (radix > Character.MAX_RADIX) {
0447 throw new NumberFormatException("radix " + radix
0448 + " greater than Character.MAX_RADIX");
0449 }
0450
0451 int result = 0;
0452 boolean negative = false;
0453 int i = 0, len = s.length();
0454 int limit = -Integer.MAX_VALUE;
0455 int multmin;
0456 int digit;
0457
0458 if (len > 0) {
0459 char firstChar = s.charAt(0);
0460 if (firstChar < '0') { // Possible leading "+" or "-"
0461 if (firstChar == '-') {
0462 negative = true;
0463 limit = Integer.MIN_VALUE;
0464 } else if (firstChar != '+')
0465 throw NumberFormatException.forInputString(s);
0466
0467 if (len == 1) // Cannot have lone "+" or "-"
0468 throw NumberFormatException.forInputString(s);
0469 i++;
0470 }
0471 multmin = limit / radix;
0472 while (i < len) {
0473 // Accumulating negatively avoids surprises near MAX_VALUE
0474 digit = Character.digit(s.charAt(i++), radix);
0475 if (digit < 0) {
0476 throw NumberFormatException.forInputString(s);
0477 }
0478 if (result < multmin) {
0479 throw NumberFormatException.forInputString(s);
0480 }
0481 result *= radix;
0482 if (result < limit + digit) {
0483 throw NumberFormatException.forInputString(s);
0484 }
0485 result -= digit;
0486 }
0487 } else {
0488 throw NumberFormatException.forInputString(s);
0489 }
0490 return negative ? result : -result;
0491 }这个是JDK的源代码
public class ConvertStringToInt { public static void main(String[] args) {
String s = "-1233444";
int result = ConvertStringToInt.myPraseInt(s);
System.out.println(result);
}
public static int myPraseInt(String s) {
if(s == null) {//如果为null直接抛出异常
throw new NumberFormatException("null");
}
int result = 0;//记录最后返回的int值
int value;//字符串中每个字符转char换成int保存在value中
int index = 0;//遍历s的每个字符
int max = s.length();//s的长度
//int limit;//允许的范围;暂时还不清楚
boolean negative = false;//是否为负数,默认为false
if(s.charAt(0) == '-') {
negative = true;
index ++;
}
while(index < max) {
value = s.charAt(index ++) - 48;
if(value<0 || value>9) {
throw new NumberFormatException("" + s.charAt(index));
} else {
result = result * 10 + value;
}
}
if(negative) {
if(index > 1) {
return -result;
} else {//只有一个负号的情况
throw new NumberFormatException(s);
}
} else {
return result;
}
}
}
重新考了了下,这个比较完善了。我也是去看源代码来完善的,于是出现了新的问题:
Integer有两个静态方法能够将String转换成int值:
public static int parseInt(String s);
public static int parseInt(String s, int radix);
说明:其中带有两个参数的方法第二个参数指的是转换成的进制,而只带一个参数的
方法则表示默认转换成十进制,因此我们不难想到带有一个参数的方法最终也应该是调用带有两个参数的parseInt方法,现在潜入JDK源代码中验证一下:
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
而带有两个参数的parseInt方法源代码如下:
public static int parseInt(String s, int radix)
throws NumberFormatException {
if (s == null) {//如果字符串为空就直接抛出异常
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {//MIN_RADIX=2
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}//jdk规定了进制的范围是2~36的范围
if (radix > Character.MAX_RADIX) {//MAX_RADIX=36
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;//存储最后返回的int值
boolean negative = false;//是否为负数,默认为false
int i = 0, max = s.length();//用i遍历s,max存储s的长度
int limit;//存储允许的int的范围,如:为负数,那么limit=Integer.MIN_VALUE
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;
//这里可能比较多人奇怪:怎么是负数,返回result?其实这是因为前面已经做了处理了,//这里的result已经是负数了。这主要是因为Integer的范围最小负数的绝对值比最大的正//数要大(大1)的原因
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
}
然后那个multmin作用是什么???
其实就是我的程序尚未实现的问题:怎么判断result是不是超出Integer范围了??
很明显,直接判断result<Integer.MAX_VALUE是不行的,因为这样永远是true的吧,所以JDK也做了处理,而我就是处理这块不太懂,希望牛人可以帮忙解答一下。
也在线等等吧。
当然还有个更复杂的问题,就是JDK后台是怎么求字符(注意是字符ch)对应的int值的??应该不会像我们那样-48吧。
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
而带有两个参数的parseInt方法源代码如下:
public static int parseInt(String s, int radix)
throws NumberFormatException {
if (s == null) {//如果字符串为空就直接抛出异常
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {//MIN_RADIX=2
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}//jdk规定了进制的范围是2~36的范围
if (radix > Character.MAX_RADIX) {//MAX_RADIX=36
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;//存储最后返回的int值
boolean negative = false;//是否为负数,默认为false
int i = 0, max = s.length();//用i遍历s,max存储s的长度
int limit;//存储允许的int的范围,如:为负数,那么limit=Integer.MIN_VALUE
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) {
//由于不是最后一位(i<max),因此如果result现在已经小于limit/radix,那么就可以直
//接判断超出Integer范围,而不用继续做下面比较复杂的判断了。
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
//此处是该算法中最为精妙的地方,因为result此时肯定是合法的Integer值,而如果加
//digit后超出Integer范围,那么现在的result肯定不能小于limit+digit;其实这也是个
//比较简答的原理:因为result-digit必须大于limit,因为result自然就应该小于
//limit+digit了,如果不这样做,那么result怎么运算都会由于二进制码的表示得到
//Integer范围内的值,但实际可能已经超出Integer范围了。
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
if (negative) {
if (i > 1) {
return result;
//这里可能比较多人奇怪:怎么是负数,返回result?其实这是因为前面已经做了处理了,//这里的result已经是负数了。这主要是因为Integer的范围最小负数的绝对值比最大的正//数要大(大1)的原因
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
}