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 static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); } 就是3楼的代码,大概看了下,就是按十进制处理字符串,先判断正负号,完后依次取出字符串中的每个字符,转换为int,用的是Character.digit(char ch, int radix)方法,最后做一些乘法减法,得到result
就是将一个String类型对象转换成int类型 用到包装类Integer 没有什么难的
public class NewInteger { public static void main(String[] args) { NewInteger ni = new NewInteger(); Integer nn = ni.parseInt("1234677"); if (nn == null) { System.out.println("字符串格式不正确!"); } else { System.out.println(nn); } } public int getAint(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; } return -1; } public int getSint(String s) { int ii = 0; if (s.charAt(0) != '-' && s.charAt(0) != '+' && getAint(s.charAt(0)) == -1) { return -1; } for (int i = 1; i < s.length(); i++) { if (getAint(s.charAt(i)) == -1) { return -1; } } if (s.charAt(0) == '-') { for (int n = 0; n < s.length() - 1; n++) { ii += getAint(s.charAt(n + 1)) * getAnumber((s.length() - n - 2)); } return ii * -1; } else if (s.charAt(0) == '+') { for (int n = 0; n < s.length() - 1; n++) { ii += getAint(s.charAt(n + 1)) * getAnumber((s.length() - n - 2)); } return ii; } else { for (int n = 0; n < s.length(); n++) { ii += getAint(s.charAt(n)) * getAnumber((s.length() - n - 1)); } return ii; } } public Integer parseInt(String s) { if (!s.equals("-1") && getSint(s) == -1) { return null; } return getSint(s); } public int getAnumber(int n) { int m = 1; for (int i = 0; i < n; i++) { m *= 10; } return m; } }
如果这道题的意思是让你自己实现String--int的转变,我觉得这种面试题就太无聊了
简单的实现 /** * 模拟Integer.praseInt()方法 * * @param str * @return */ public static int praseInt(String str) { int ret = 0; for (int i = str.length() - 1, j = 0; i >= 0; i--, j++) { char ch = str.charAt(i); if (ch != '-') { if (ch < '0' || ch > '9') throw new NumberFormatException(); ret += (int) (ch - '0') * Math.pow(10, j); } else { ret -= 2 * ret; } } return ret; }
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;
}
}源码
return parseInt(s,10);
}
就是3楼的代码,大概看了下,就是按十进制处理字符串,先判断正负号,完后依次取出字符串中的每个字符,转换为int,用的是Character.digit(char ch, int radix)方法,最后做一些乘法减法,得到result
用到包装类Integer
没有什么难的
NewInteger ni = new NewInteger();
Integer nn = ni.parseInt("1234677");
if (nn == null) {
System.out.println("字符串格式不正确!");
} else {
System.out.println(nn);
} } public int getAint(char c) {
switch (c) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
}
return -1;
} public int getSint(String s) {
int ii = 0;
if (s.charAt(0) != '-' && s.charAt(0) != '+'
&& getAint(s.charAt(0)) == -1) {
return -1;
}
for (int i = 1; i < s.length(); i++) {
if (getAint(s.charAt(i)) == -1) {
return -1;
}
}
if (s.charAt(0) == '-') {
for (int n = 0; n < s.length() - 1; n++) {
ii += getAint(s.charAt(n + 1))
* getAnumber((s.length() - n - 2));
}
return ii * -1;
} else if (s.charAt(0) == '+') {
for (int n = 0; n < s.length() - 1; n++) {
ii += getAint(s.charAt(n + 1))
* getAnumber((s.length() - n - 2));
}
return ii;
} else {
for (int n = 0; n < s.length(); n++) {
ii += getAint(s.charAt(n)) * getAnumber((s.length() - n - 1));
}
return ii;
}
} public Integer parseInt(String s) {
if (!s.equals("-1") && getSint(s) == -1) {
return null;
}
return getSint(s);
} public int getAnumber(int n) {
int m = 1;
for (int i = 0; i < n; i++) {
m *= 10;
}
return m;
}
}
/**
* 模拟Integer.praseInt()方法
*
* @param str
* @return
*/
public static int praseInt(String str) {
int ret = 0;
for (int i = str.length() - 1, j = 0; i >= 0; i--, j++) {
char ch = str.charAt(i);
if (ch != '-') {
if (ch < '0' || ch > '9')
throw new NumberFormatException();
ret += (int) (ch - '0') * Math.pow(10, j);
} else {
ret -= 2 * ret;
}
}
return ret;
}