Results 1 to 9 of 9
  1. #1
    mitty is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default Comparing arrays

    I have the following code

    Java Code:
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class SQL {
    	
    	
    	public static void main(String[] args)throws IOException{
    
    		
    	String text =  JOptionPane.showInputDialog("Please enter your sentence here");
        String text2= null;
        String Text3 = "";
        StringTokenizer st;
        String[] keyword = {"employees", "managers"};   
        
    	st = new StringTokenizer (text, " ");
    		
    		while (st.hasMoreElements()){
    	    	  text2 = (st.nextToken());
    	    	  System.out.println (text2);
    	    	 
    	    	  Text3 = Text3 + " " + text2;
    	    	 }
    		String[] words = Text3.split (" ");
    	//System.out.println(words);
    	
    	for (int i=0; i < words.length; i++){
    				
    	  System.out.println (words[i]);
    	  	}
    	
          	}
    
    	
    }
    I'm trying to compare two arrays. The first array is the user input, for example can we use the followiing

    'show all employees who are managers'

    I have the second array which is called keyword. I want the keyword array to search through the user input (which is stored as an array now) and identify if both keywords are mentioned by the user?

    I think its an if statement but I'm not sure how to go about it.

    I had the following structure in mind

    if userinput array was equal to keyword1 AND userinput array was equal to keyword 2
    then do this.

    its just the array searching which I'm struggling with.

    Does anyone have any ideas that might be able to help as I can only find examples of array comparison to see if two arrays are identical.

    /Mitty

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    here's an algorithm outline:
    Java Code:
    matchCounter = 0
    for each String k in keywords array:
      for each String w in words array:
        if k equals w:
          matchCounter ++;
    
    if matchCounter >= 2:
      // whatever you need to do

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by iluxa View Post
    here's an algorithm outline:
    Java Code:
    matchCounter = 0
    for each String k in keywords array:
      for each String w in words array:
        if k equals w:
          matchCounter ++;
    
    if matchCounter >= 2:
      // whatever you need to do
    I think that this algorithm will consider the words array "valid" if contains two or more of only one of the Strings in the keywords array which may not be what you want. You may wish to use two boolean variables, one for each String in the keywords array and in your iteration set each boolean if its corresponding String is found. Then at the end check if both are true.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    True that. I kinda wish they'd covered hash tables... ;)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Any Collection such as a simple ArrayList has the contains(...) method, and that could be one way to solve this problem.

  6. #6
    mitty is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I think that this algorithm will consider the words array "valid" if contains two or more of only one of the Strings in the keywords array which may not be what you want. You may wish to use two boolean variables, one for each String in the keywords array and in your iteration set each boolean if its corresponding String is found. Then at the end check if both are true.
    How would I go about this? I understand the concept, just a matter of implementing it. Is there an example you could point me to so I could go through it and then try to implement it into my own work?

    I altered the previous code to the following

    Java Code:
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class SQL {
    	
    	
    	public static void main(String[] args)throws IOException{
    
    		
    	String text =  JOptionPane.showInputDialog("Please enter your sentence here");
        String text2= null;
        String Text3 = "";
        StringTokenizer st;
        String[] keyword = {"employees", "managers"};   
        
    	st = new StringTokenizer (text, " ");
    		
    		while (st.hasMoreElements()){
    	    	  text2 = (st.nextToken());
    	    	  System.out.println (text2);
    	    	 
    	    	  Text3 = Text3 + " " + text2;
    	    	 }
    		String[] words = Text3.split (" ");
    	//System.out.println(words);
    	
    	for (int i=0; i < words.length; i++){
    				
    	  System.out.println (words[i]);
    	  	
    	int matchCounter = 0;
    	for ( String k : keyword)
    		for (String w: words) 
    			if (k == w){
    				matchCounter ++;
    				if (matchCounter >=2) {
    					System.out.println("SELECT * FROM employees WHERE Position = 'Manager'");			
    							}
    			
    			}
    		
    	
    }
    			}
    	
    		
          	}
    but here was still no luck. That was he original suggestion by iluxa.

  7. #7
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    "k==w" doesnt work. Try "k.equals(w)" and you should be good

  8. #8
    mitty is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    "k==w" doesnt work. Try "k.equals(w)" and you should be good
    Thanks for that, I've altered the code now to the following,

    Java Code:
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class SQL {
    	
    	
    	public static void main(String[] args)throws IOException{
    
    		
    	String text =  JOptionPane.showInputDialog("Please enter your sentence here");
        String text2= null;
        String text3 = "";
        StringTokenizer st;
        String[] keyword = {"employees", "managers"};   
        
    	st = new StringTokenizer (text, " ");
    		
    		while (st.hasMoreElements()){
    	    	  text2 = (st.nextToken());
    	    	  System.out.println (text2);
    	    	 
    	    	  text3 = text3 + " " + text2;
    	    	 }
    		String[] words = text3.split (" ");
    	//System.out.println(words);
    	
    	for (int i=0; i < words.length; i++){
    				
    	  System.out.println (words[i]);
    	  	
    	int matchCounter = 0;
    	String querycode;
    	
    		for ( String k : keyword){
    			
    			for (String w: words) {
    				
    				if (k.equals(w)){
    					matchCounter ++;
    				}
    				
    				if (matchCounter ==2) {
    					//System.out.println("SELECT * FROM employees WHERE Position = 'Manager'");	
    					querycode = ("SELECT * FROM employees WHERE Position = 'Manager'");
    					System.out.println(querycode);
    				}
    					
    			}
    		}
    		
    	}
    	}
    }
    a query which include BOTH employees and managers now returns:

    Java Code:
    which
    employees
    are
    managers
    
    SELECT * FROM employees WHERE Position = 'Manager'
    which
    SELECT * FROM employees WHERE Position = 'Manager'
    employees
    SELECT * FROM employees WHERE Position = 'Manager'
    are
    SELECT * FROM employees WHERE Position = 'Manager'
    managers
    SELECT * FROM employees WHERE Position = 'Manager'
    I was wondering why the statement has been returned that many times instead of once. I tried to move it out of the for loop but this did not work.

  9. #9
    mitty is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Thumbs up

    It's OK now. I played around with the code and came out with the following.

    Java Code:
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class SQL {
    	
    	
    	public static void main(String[] args)throws IOException{
    
    		
    	String text =  JOptionPane.showInputDialog("Please enter your sentence here");
        String text2= null;
        String text3 = "";
        StringTokenizer st;
        String[] keyword = {"employees", "managers"};   
        String[] keyword2 = {"employees", "admins"};
        int key = 0;
    	st = new StringTokenizer (text, " ");
    	int count;
    	  
    		
    		while (st.hasMoreElements()){
    	    	  text2 = (st.nextToken());
    	    	  System.out.println (text2);
    	    	 
    	    	  text3 = text3 + " " + text2;
    	    	 }
    		String[] words = text3.split (" ");
    	//System.out.println(words);
    	
    	for (int i=0; i < words.length; i++){
    				
    	  System.out.println (words[i]);
    		  
    	  
    	  
    	count = words.length;   
    /*	boolean found = false;
    	if (found == false) {*/	
    	
    	  while (i < count){
    		  String Word = words[i];
    		  if (Word.equals ("managers")){
    			  key ++;
    		     }
    		  else if (Word.equals ("employees")){
    			  key ++;
    		  }
    		       i++;
    		  }
    	   if (key == 2){
    		   System.out.println("SELECT * FROM employees WHERE Position = 'Manager'");
    		   /*found = true;*/
    		   key = 0;
    		   i=0;
    	   }
    	   else {
    			key= 0;  
    			i=0;
    	   }	   
    	   
    	   while (i < count){
    			  String Word = words[i];
    		  if (Word.equals ("admins")){
    				  key ++;
    			     }
    		  else if (Word.equals ("employees")){
    				  key ++;
    			  }
    			       i++;
    			  }
    	  if  (key == 2){
    	       System.out.println("SELECT * FROM employees WHERE Position = 'admin'");
    		  /* found = true;*/
    		   key = 0;   
    	   }
    	   else {
    			key= 0;      
    	   }
    	   
    	}
    			
    	}
    	/*}*/
    
    	
    }
    Thanks for all your help guys :)

Similar Threads

  1. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  2. Comparing two Char arrays
    By viperlasson in forum New To Java
    Replies: 3
    Last Post: 01-30-2010, 09:05 AM
  3. comparing arrays..
    By circuspeanuts in forum New To Java
    Replies: 5
    Last Post: 05-25-2009, 08:05 AM
  4. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 10:40 AM
  5. Comparing JavaWebFrameworks
    By pegitha in forum Web Frameworks
    Replies: 1
    Last Post: 05-18-2007, 07:23 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
  •