Please consider the following code:
import java.io.*; //Sorts by dividing the array in 2 groups then joining them
public class Esercizio29 {static void join(char[] a, int l, int m, int u) {
char[] b = new char[u - 1 + 1];
int i = l, j = m + 1, k = 0;
while (i <= m && j <= u)
if (a[i] <= a[j])
b[k++] = a[i++];
else
b[k++] = a[j++];
while (i <= m)
b[k++] = a[i++];
while (j <= u)
b[k++] = a[j++];
for (k = 0; k <= u - l; k++)
a[k + l] = b[k];
}
//Sorts the array from l to u
static void sort(char[] a, int l, int u) {
int m;
if (l != u) {
m = (l + u) / 2;
sort(a,l,m);
sort(a,m + 1,u);
join(a,l,m,u);
}
}
public static void main(String[] args) throws IOException{
final int N = 16;
char temp, v[] = new char[N];
for (int i = 0; i < N; i++)
v[i] = (char) System.in.read();
sort(v, 0, N - 1);
System.out.println("Vettore ordinato: ");
for(int i = 0; i < N; i++)
System.out.print(v[i]);
System.out.println();
}}
After running this code it gives me this result:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Esercizio29.fondi(Esercizio29.java:14)
at Esercizio29.ordina(Esercizio29.java:27)
at Esercizio29.ordina(Esercizio29.java:25)
at Esercizio29.ordina(Esercizio29.java:25)
at Esercizio29.ordina(Esercizio29.java:25)
at Esercizio29.main(Esercizio29.java:39)
What does this error mean and how do I solve it? thank you.