Need help with assignment
I've been banging my head over this for a couple days now and I just can't figure it out. I'm supposed to read a set of integers into an array and then find the duplicates and display the locations of the duplicate integers. I was thinking that creating two arrays with the same integers then comparing would be the solution, but I have no idea how to set that up. As you can see getting the integers into the array was fairly simple, but any help with getting started with the rest would be greatly appreciated.
Code :
public static void dupInteger(String filename){
try{
Scanner sc = new Scanner(new File(filename));
int [] numArray = new int[100];
int n = 0;
while(sc.hasNextInt()){
if(n >= numArray.length ){
System.err.println("Too much data");
System.exit(1);
}
numArray[n] = sc.nextInt();
//System.out.println(numArray[n]);
n++;
}
for(int i = 0; i <= numArray.length; i++)
System.out.println(numArray[i]);
}
catch (IOException e){
System.out.println(e);
}
}
Re: Need help with assignment
you may be able to use the binary search function "public static int binarySearch(short[] a, short key)" short[] a is the array to search in and short key is the value to search for. The only catch is that the array has to be sorted with the .sort.
Re: Need help with assignment
Yes this definitely worked for me, so all you would have to do is adapt this little bit of code to your use.
Code :
int[] myArray = {1, 2, 3, 6, 8, 20, 25, 36, 39};
int position = java.util.Arrays.binarySearch(myArray, 36);
System.out.println("Number 36 is at position " + position);