parseint (atoi)구현

Algorithm 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

'Algorithm' 카테고리의 다른 글

성능 체크  (0) 2016.09.01
length() 메소드 작성  (0) 2015.06.23
소인수 분해 구하기  (0) 2015.05.24
너비우선탐색 과 깊이우선탐색  (0) 2015.05.20
피보나치 동적계획법 소스  (0) 2015.05.20
Posted by 이상욱1
,