domingo, 25 de julho de 2010

[java] convertStringArrayToIntArray

convertendo um array de strings de em um array de int:




/**
* The function stringArrayToIntArray takes as input a String array which is assumed to
* contain Strings that represent ints. It returns an int array containing the ints
* represented by each String from the String array.
*
* @param s
* @return int[]
*/
public static int[] convertStringArrayToIntArray(String[] s) {
try {
int[] intArray;
try {
intArray = new int[s.length];
} catch (NullPointerException e2) {
return null;
}
for (int i = 0; i < s.length; i++) { try {
intArray[i] = Integer.parseInt(s[i]);
} catch (Throwable e) {
try {
if (s[i].equals("")) {
intArray[i] = 0;
continue;
}
} catch (NullPointerException e1) {
intArray[i] = 0;
continue;
}
throw e;
}
}
return intArray;
} catch (Throwable e) {
throw new RuntimeException(
"JavaUtils.convertStringArrayToIntArray() - Error on convert the String array \"" +
s + "\" to an int array. Error details: " + e);
}
}

Nenhum comentário: