I am trying to create a program that sort five integers in ascending order. I have never ran into a dereferenced error before so I am curious what I am doing wrong.
Scanner input = new Scanner(System.in);
int[] a = new int[5];
for (int i = 0; i < 5; i++) {
System.out.println("Please enter integer # "+ 1 + i);
int temp = input.nextInt();
a[i] = temp;
}
System.out.println("Sorted from lowest to highest");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int temp = a[i];
int tempB = a[j];
if (temp.compareTo(tempB) < 0) {
a[i] = tempB;
a[j] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
System.out.println(a[i]);
}
}
}
I am getting the error on this line.
if (temp.compareTo(tempB) < 0)
Thanks!