Algorithm
parseint (atoi)구현
이상욱1
2015. 6. 24. 17:16
public class Strparsei {
public static int atoi(String str) {
int radix = 10; // 이게 돌아가면서 원래 값에서 자리수 한자리를 늘여준다
byte[] temp = str.getBytes();
int result = 0;
for(int i=0;i<temp.length;i++) {
if (temp[i] < '0' || temp[i] > '9') { // 0~9 넘어갈경우 (문자 방지)
throw new NumberFormatException();
}
result = (result*radix) + temp[i] - '0';
}
return result;
}
public static void main(String[] args) {
int a=0;
a=Strparsei.atoi("1234");
System.out.println(a);
}
}
참고사이트 http://globalbiz.tistory.com/m/post/82