Ok.
Ho notato che nel metodo non effettui i controlli sulla correttezza semantica dell'orario: cioè, non verifichi che le ore siano nel range [0..23] e che minuti e secondi siano nel range [0..59].
Io ora ti propongo due alternative. Una più o meno equivalente alla tua (che non effettua controlli semantici) senza usare il meccanismo delle eccezioni; un'altra che, invece, usa il meccanismo delle eccezioni ed in più effettua anche i controlli semantici di cui sopra. Prova a vedere se riesci a capire come funzionano.
Ah... in entrambe le versioni il separatore usato nella stringa viene passato come argomento (come RegEx), così da rendere la funzione più generale.
Versione 1:
codice:
private static int[] scanTime(String strOrario, String separatore) {
int[] nums = {0, 0, 0};
String[] hhmmss = strOrario.split( separatore );
try {
for(int i=0; i<3; i++) nums[i] = Integer.parseInt( hhmmss[i] );
} catch (NumberFormatException nfe) { /* ignoro */ }
return nums;
}
Versione 2:
codice:
private static int[] scanTime(String strOrario, String separatore) throws NumberFormatException {
int[] nums = new int[3];
String[] hhmmss = strOrario.split( separatore );
for(int i=0; i<3; i++) nums[i] = Integer.parseInt( hhmmss[i] );
// Controlli numerici
if ((nums[0] < 0) || (nums[0] > 23)) throw new NumberFormatException("Valore non valido per le ore: " + hhmmss[0]);
if ((nums[1] < 0) || (nums[1] > 59)) throw new NumberFormatException("Valore non valido per i minuti: " + hhmmss[1]);
if ((nums[2] < 0) || (nums[2] > 59)) throw new NumberFormatException("Valore non valido per i secondi: " + hhmmss[2]);
return nums;
}
Ciao.