I'm trying to create a code to prevent duplicate array elements. Is there anyway I can do this without creating an arrayList?
When running the program this error occurs when I enter the first #:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at DuplicateElimination.main(DuplicateElimination.java:32)
Here is my code:
int[] numList = new int[5];
int newValue;
boolean invalid;
for (int i = 0; i < numList.length; i++){
do{
System.out.print("Please enter number");
System.out.println(" ");
newValue = input.nextInt();
//This is where the error occurs when I try to compare
//The last list element to the input value
invalid = numList[i-1] == newValue;
if(newValue < 10 || newValue > 100){
System.out.print("Invalid number, Please enter a number between 10 and 100");
newValue = input.nextInt();
}
if(invalid){
System.out.print("That number was entered already try again");
}
}while(invalid);
insertIntoArray(numList, i, newValue);
printArray(numList);
}