I'm making a program where i calculate a random number using a function named generarAleatorio.
public static int generarAleatorio(int l){
java.util.Random X = new java.util.Random();
return X.nextInt(l) ;
}
Then, I use that number to locate a value inside an array using the length of the array as the parameter for the generarAleatorio parameter
public static String generarFecha(){
int[] dias={1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30};
int[] mes={1,2,3,4,5,6,7,8,9,10,11,12};
return String.format("%d-%d",dias[generarAleatorio(dias.length)],
mes[generarAleatorio(generarAleatorio(mes.length))]);
}
The problem is that sometimes an Exception is showed that looks like this
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388)
at generadorregistros.GeneradorRegistros.generarAleatorio(GeneradorRegistros.java:70)
at generadorregistros.GeneradorRegistros.generarFecha(GeneradorRegistros.java:108)
at generadorregistros.GeneradorRegistros.main(GeneradorRegistros.java:48)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
It tells me that the Bound im sending its not positive, but, as far as i know, the length of the Array is always the same and also positive.
What's the cause of this? Is the length of the array not always positive?
Aucun commentaire:
Enregistrer un commentaire