Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with assignment

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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);
    		}
    	}
    Last edited by helloworld922; February 21st, 2010 at 01:31 AM.


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

            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);

Similar Threads

  1. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  2. need help in my assignment guys :(
    By Mr.cool in forum Collections and Generics
    Replies: 7
    Last Post: December 28th, 2009, 08:30 AM
  3. New assignments for JAVA beginners
    By Peetah05 in forum File I/O & Other I/O Streams
    Replies: 24
    Last Post: April 24th, 2009, 12:33 PM
  4. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 04:14 AM
  5. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 10:19 AM