csv 파일 콤마로 구분하여 저장하다가 마지막 빈 값때문에 IndexOutOfBoundsException

Hue·2023년 9월 18일

노트 메모 이것저것

목록 보기
18/18

csv 파일을 저장할때 문자열 사이에 콤마를 구분하였고, 마지막 콤마가 들어가는 것은 신경쓰지 않았다.
(예를 들어, "red","green","blue","yellow",)
그런데 이 파일을 받아와서 읽고 배열에 담으니 마지막 값이 없어 자꾸 IndexOutOfBoundsException이 나는 것이었다...

받아올때 해당 인덱스를 지워주는 방법도 있겠지만, 애초에 불필요한 값을 저장하지 않도록 수정하기로 했다.


val color= arrayListOf<String>("red","green","blue","yellow")

val builder = StringBuilder()
for (color in colorList) build.append("$color,")
// "red","green","blue","yellow",

builder.deleteCharAt(builder.lastIndex)
// "red","green","blue","yellow"

0개의 댓글