주어진 문자열을 int형으로 변환

Devel/알고리즘(Algorithm)|2022. 1. 14. 15:29
반응형

[JAVA] 주어진 문자열을 int형으로 변환하기

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class StringParseToInt {


    @Test
    public void toIntFromString() {
        assertThat(toIntFromString("123"), is(123));
    }

    private int toIntFromString(String str) {
//        return Integer.parseInt(str);
        char[] strArr = str.toCharArray();
        int base = 0;
        for (char c : strArr) {
            base *= 10;
            base += c - '0';
        }
        return base;
    }
}

'Devel > 알고리즘(Algorithm)' 카테고리의 다른 글

백준 [2798] - 블랙잭 풀이  (0) 2023.02.22
백준[2920] - 음계 풀이  (0) 2023.02.21
댓글()
loading