Results 1 to 16 of 16
  1. #1
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default HELP!!! Problems with Array: matching, sorting, etc

    So here's my dilemma. I am suppose to have 2 sets of arrays that the user inputs. The program will have to sort through the arrays and check for duplicate integers. For example, if an array has a set [2,3,4,4,5] , it is equivalent to [2,3,4,5]. THEN, it tries to match with the second set of integers and check whether they're the same [2,3,4] [4,3,2] (order does not matter) ; different [2,3,4] [5,6,7] . There's more combinations but I am trying to go at it one by one. If you guys help me out here I would gladly appreciate it!

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    
    public class MatchingIntegers {
      public static void main(String[] args) {
      
        final int NUMBERS_IN_SET = 10;
        int[] numbers1 = new int[NUMBERS_IN_SET];
        int[] numbers2 = new int[NUMBERS_IN_SET];
        
    	 Scanner input = new Scanner(System.in);
    	 
    	 System.out.println("Enter the first set of numbers individually (#1 - 10)\n");
      
        // Prompt user to input the first set of numbers
        for (int i = 0; i < numbers1.length; i++) {
        System.out.print("Enter a number: ");
    	 
    	   // Convert string into integer
    		numbers1[i] = input.nextInt();
        }
    
    	 System.out.println("Enter the second set of numbers individually (#1 - 10).\n");
    	
    	 // Prompt user to input the second set of numbers
      	 for (int i = 0; i < numbers2.length; i++) {
        System.out.print("Enter a number: ");
         
    	   // Convert string into integer
    		numbers2[i] = input.nextInt();
        }
    	 
    	// Sort the whole arrays for both sets 
    	java.util.Arrays.sort(numbers1);
    	java.util.Arrays.sort(numbers2);
    
    // perform a linear search on the data
    
    
    
      
    	
    	// Prepare the result
    	String output = " The first array is : ";
    	for (int i = 0; i < numbers1.length ; i++) {
    	  output += numbers1[i] + " ";	
    	}
    	
    	// Display the result
    	System.out.println(output);
    	
    	output = "The second array is : ";
    	for (int i = 0; i < numbers2.length ; i++) {
    	  output += numbers2[i] + " ";	
    	}
    		
    	// Display the result
    	System.out.println(output);
    
      }
    }
    Last edited by jrelvi23; 11-19-2009 at 12:22 AM.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    So, what do you need help with, specifically? - it's not obvious from your post.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    I need help on how I could look for duplicate numbers in each array and then how can I compare them to each other. Are both arrays the same? are they different?

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    For all of them, I would say sort the array first - it makes the job MUCH easier.

    Then, look at a sorted listed, like the example you gave "[2,3,4,4,5]" and notice what happens to the duplicates.


    For the second question, it seems that order and duplicates don't matter - thus, you are describing a Set. Again, sorting will make this much easier. Compare two sorted sets and see what you notice. How can you tell if the sets are same when there are no duplicates? When there are duplicates?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    I found the way to sort them out... Check the new code. I added two lines above //Display the results. Now, it prints out the array in ascending order which is nice. As for the duplicates, it just lines them up next to each other... Not sure if that is what you meant by "notice what happens..."

    I need help on how to compare them to each other. After the duplicates are ignored, it prints out the new array without the duplicates. See if they are the same array, then it suppose to print, "Both arrays are identical." If they're not... you get the picture. This is a part that makes me go "AAHHH!!" and I'm a sure it's probably a small code too which aggravates me!

    Thanks btw!

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Exactly, the duplicates line up in a sorted array, which makes it very easy to detect (and remove) them.


    As for the second part, are you suppose to check if two different arrays are equal, or if the array (with duplicates) is equal to the same array without duplicates?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #7
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    How do you remove the duplicates exactly? I am suppose to check if the two different arrays are equal AFTER the duplicates have been removed.

  8. #8
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    You don't have to remove the duplicates to determine if the arrays are equal without duplicates.

    That being said, if you want to remove the duplicates, the easiest way would be to add the unique items to a List, and then convert the list to an array. Are you allowed to use a list? You can do it with only arrays, if that's required.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  9. #9
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    we could use a list. coud i see both ways though? for future reference.

  10. #10
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Well, how would you do it? If you had a list of values that are sorted, how could you obtain a list of unique values?

    For example, given the values "[2,3,4,4,5]", how would you turn it into "[2,3,4,5]" (removing the duplicate 4)? It's usually best to describe the steps first, and once you understand them, then write code that performs those same steps.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  11. #11
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    lol... I'm sure you gave me a hint there for unique values but I'm still not sure....

    Am I suppose to count the occurences of each letter and if there's more than 1, do not allow it to print again...

    there's a section in the textbook that looks like it might point me to the right direction using the linear search approach. But the problem is that it compiles correctly, but then I get an error that says:

    Here's the code from the book.

    Java Code:
    public class LinearSearcher {
    
      public static int linearSearch(int[] list, int key) {
        for (int i = 0; i < list.length; i++) {
    	   if (key == list[i])
    		  return i;
    		  
    	 }
    	 return -1;
      }
    }
    This is the error code: ----jGRASP exec: java LinearSearcher

    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    Is this even helpful to me to try to figure out?
    Last edited by jrelvi23; 11-18-2009 at 09:15 PM.

  12. #12
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    That last snippet you posted just checks if key is in list and returns -1 if it is. It wont work because you're missing the main method that calls it, populates the list, picks the key etc
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  13. #13
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    Thanks for that! I am pretty sure I need to use a linear search method. Being new to java... I am not sure how to use it into my code....

  14. #14
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    linear search just checks every item in the list until it finds the item or runs out of things to check. just a basic loop can cover it.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  15. #15
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    Ok well thanks guys. I'll try to figure it out and let you guys know.

  16. #16
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    you cant run linear searcher you have to instiate that class like you would a java class inside the class that has your MAIN METHOD

    Java Code:
     
    package arrays;  //you need to have a folder called arrays and every class
                           //you create has to start with this package arrays line
                           //compile each .java separately 
                           //then run the .class that has the main in it 
                           //in this case my entry point is DO_StUFF
    public class DO_STUFF 
            {
              
            DO_STUFF(){}// whatever the class needs to do
    
    public static void main(String args[])
    { int [] myArray = new int [4];
        for(i= 0; i<4; i++)
         myArray[i] = (i + 1);
      LinearSearcher L = new LinearSearcher();
       L.linearsearch(myArray, 7);
    }
    }
    Last edited by aaroncarpet; 11-19-2009 at 12:19 AM.

Similar Threads

  1. Storing high score and sorting the array
    By Implode in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 01:43 AM
  2. [SOLVED] Sorting array in descending order?
    By dan0 in forum New To Java
    Replies: 14
    Last Post: 04-16-2009, 01:19 AM
  3. Converting array to list and sorting it
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:36 PM
  4. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:39 PM
  5. Replies: 0
    Last Post: 04-14-2008, 09:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •