Я очень запутался с ArrayIndexOutOfBoundsException
Я получаю.Я не могу сделать массив TempCity
значений, выделенных после использования метода split.
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];
}