10 Replies Latest reply: Dec 18, 2006 7:34 AM by 807607 RSS

    Having trouble searching a String array

    807607
      Hi, I hope someone can help me with a problem that I�m having.

      I want to create a program in which I can:
      1. Type in a combination of letters.
      2. Compare the typed-in letters to a number of words that are stored in a string array.
      3. If the combination of letters match one or more of the words, or part of the words, found in the string array, these words are to be returned.

      Needless to say I�m fairly new to java, so any help that you can give me is very much appreciated.

      My code looks as follows right now, and I'm not sure that I�m using the regionMatches function properly.

      Thanks!

      /attans

      import javax.swing.*;
      
      import java.util.*;
      
      import java.text.*;
      
      import java.awt.*;
      
      
      public class Words {
           
           static Collator co = Collator.getInstance();  
                                    
           public static void main(String[] arguments) {
                
                String indata;
      
                 co.setStrength(Collator.PRIMARY); 
                
      
                indata = JOptionPane.showInputDialog("Type in the text you have: ");
                
                if (indata == null) {
      
                     System.exit(0);
      
                }
       
      
                String[] arkiv = { "Hello", "Goodbye", "Carpet", "House", "Sand", "Bookshelf", "Train" }; // examples of words
      
                Arrays.sort(arkiv);     
      
                int arkivLangd = arkiv.length();
                
      
                boolean foundIt = false;
      
                   for (int i = 0; i < arkiv.length; i++) {
      
                     if (arkiv.regionMatches(true, i, indata, 0, arkivLangd)) { // true
           
                     foundIt = true;
      
                            System.out.println("Possible words are: " + i);
           
                     break;
                     
                     }
      
                     if (!hittat) System.out.println("Possible words are:" + "No words found");
      
                }
      
           }
      
      }
        • 1. Re: Having trouble searching a String array
          807607
          I think you need to use the String.contains(CharSequence ) method rather than regionMatches().

          P.S. A String IsA CharSequence so the method can accept a String.
          • 2. Re: Having trouble searching a String array
            791266
            @Op, you do also have a few compilation errors. length is an attribute of an array, and not a method, and the method contains or indexOf should be invoked on one instance in the array, and not on the array itself.

            Kaj
            • 3. Re: Having trouble searching a String array
              807607
              if (arkiv.regionMatches(true, i, indata, 0, arkivLangd))
              if (arkiv.regionMatches(true,0, indata, 0, arkivLangd))
              • 4. Re: Having trouble searching a String array
                807607
                Ok, I�ll look into that and see if I can get it to work.

                Thanks.

                /attans
                • 5. Re: Having trouble searching a String array
                  791266
                  Oh. Btw. It's easier for others to understand your code if you use English instead of Swedish in the code :)

                  Kaj
                  • 6. Re: Having trouble searching a String array
                    807607
                    Oh. Btw. It's easier for others to understand your
                    code if you use English instead of Swedish in the
                    code :)
                    So that is where I have been going wrong!
                    • 7. Re: Having trouble searching a String array
                      807607
                      But I thought Swedish was a universal language ;-).

                      Will keep that in mind next time.

                      Still trying to get it to work properly, but not quite there yet. Thanks for all your input though!

                      /attans
                      • 8. Re: Having trouble searching a String array
                        807607
                        Ok, time for me to ask for help again.

                        I�ve tried using the String.contains(CharSequence ) method recommended by Sabre 150 instead of the regionMatches method, but it doesn't seem to work for me. No matter what sequence of characters I put in, I get the "No words found" response.

                        I've also modified the code to better suit non-swedish speaking java people.

                        I get that it is the if-statement after the for-loop that is not doing what I want. Any ideas on another method that I can use?

                        Thanks!

                        /attans
                        import javax.swing.*;
                        
                        import java.util.*;
                        
                        import java.text.*;
                        
                        import java.awt.*;
                        
                        
                        
                        public class Words2 {
                             
                             static Collator co = Collator.getInstance();  
                                                      
                             public static void main(String[] arguments) {
                                  
                                  String input;
                        
                                   co.setStrength(Collator.PRIMARY); 
                                  
                        
                                  input = JOptionPane.showInputDialog("Type in the text you have: ");
                                  
                                  if (input == null) {
                        
                                       System.exit(0);
                        
                                  }
                         
                        
                                  String[] archive = { "Shaft", "Draft", "Craft", "Lift", "Gift", "Shift", "Drift" }; // examples of words
                        
                                  Arrays.sort(archive);     
                        
                                     for (int i = 0; i < archive.length; i++) {
                        
                                       if (archive.contains(input)) // true
                             

                                  System.out.println("Possible words are: " + i);


                                       else

                                            System.out.println("No words found");
                             
                                       break;
                                       

                                  }

                             }

                        }
                        • 9. Re: Having trouble searching a String array
                          807607
                          Take the 'break' out of the loop. I suspect you don't want to break until all the list has been processed
                          • 10. Re: Having trouble searching a String array
                            807607
                            Thanks!
                            You�re right, of course I want it to loop through the entire array.
                            Works much better now.

                            /attans