정답
public int getStrToInt(String str) {
int result = 0;
int sign = 1;
int index = 1;
char ch = str.charAt(0);
if(ch == '-')
sign = -1;
else if(ch == '+')
sign = 1;
else
index = 0;
for (int i = index; i < str.length(); i++) {
result = result * 10 + (str.charAt(i)-'0');
}
return sign * result;
}
해설
문제를 해결하는 방법은 많습니다. 그중 두 가지 방법을 알아보겠습니다. 첫 번째는 자바가 제공하는 api를 사용하지 않고 직접 풀어보는 방법이고, 두 번째는 자바가 제공하는 api를 이용해 간단하게 풀어보는 방법입니다.