매우 혼란 스럽습니다. ArrayIndexOutOfBoundsException
입니다. 분할 방법을 사용한 후에 배열 TempCity
의 값을 할당 할 수 없습니다.문자열에 대한 분할 작업 후 ArrayIndexOutOfBoundsException 오류가 발생했습니다.
String[] TempCity = new String[2];
cityNames = props.getProperty("city.names").split(",");
cities = new City [cityNames.length];
//I have also tried String[] TempCity without succes
for (int i = 0; i < cities.length; i++) {
System.out.println(TempCity[1]); //OK
TempCity = cityNames[i].split(":"); // returns String array, problem is when Strings look like "something:" and do not receive second value of array
System.out.println(TempCity[1]); //Error
try{
if (TempCity[1] == null){}
}
/* I was thinking about allocating second array's value in catch */
catch (Exception e)
{
TempCity[1] = new String();
//I'm getting Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException: 1
}
try{
cities[i] = new City(TempCity[0], TempCity[1]);
...
도움 주셔서 감사합니다! 는 지금, 내 솔루션은 다른 문자열 배열을 만드는 작업이 포함됩니다 :
String[] temp = new String[2];
String[] tempCity = new String[2];
temp = cityNames[i].split(":");
for (int j = 0; j < temp.length; j++){
tempCity[j] = temp[j];
}