문자열을 받았을 때 시작점이 구분 기호 앞에있는 숫자를 반환하는 함수를 작성하려고합니다. (', ')
. 따라서 넘겨 준 경우 ("45,621,9", 0)
은 45
을 반환합니다.StringIndexOutOfBoundsException - 처리
이것은 내가 가지고있는 것으로, 문자열 끝 부분을 치기 전까지는 StringIndexOutOfBoundsException
을 String index out of range: 8
으로 던집니다.
따라서 ("45,621,9", 7)
으로 시도하면 오류가 발생합니다. 어떻게 문자열의 끝을 처리하고 루프를 멈출 수 있습니까? 내가 null
체크를 사용한다고 생각했지만 char
이래로 나는 할 수 없다.
final char delim = ',';
char dataStorage;
int nextItem (String data, int startFrom) {
String dataValue = "";
for (int i = startFrom; i < delim; i++) {
dataStorage = data.charAt(startFrom);
if (dataStorage != delim) {
dataValue = dataValue + dataStorage;
startFrom ++;
}
}
startFrom++;
return Integer.parseInt(dataValue);
}
는 지금 내가 nextItem("45,621,9", 7)
를 호출하면 나는 9
를 반환하고 루프를 중지해야하는 예외를 얻을.
루프 조건이 정말 이해가되지 않습니다 :'에 대한'(INT I = startFrom; 나는
언어 이름으로 태그를 추가하십시오 – derloopkat