9 Replies Latest reply on Nov 19, 2007 4:01 AM by 807600

    Compare substrings in strings

    807600
      Not sure if this bit of code is what I'm suppose to be doing - hopefully someone can point me int he direction of the thing to think about

      */***
      ** Returns the number of times a given substring appears within the strings of a list of strings*
      ** @param listOfStrings the ArrayList that contains the strings to check*
      ** @param keyword the substring to find in each of the strings*
      ** @return the number of times keyword appear in the strings listed in listOfStrings*
      **/*
       public int keywordCount(ArrayList listOfStrings, String keyword) {
           int counter;
           for(int i = 0; i < listofStrings.size(); i ++){
      
                if (listofStrings.get[i] == keyword){
                     counter += 1;
                
                }
      
      }
                return System.out.println("keyword appears " + counter " " times.");     
         
         
         
       }{code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: Compare substrings in strings
          807600
          return System.out.println("keyword appears " + counter " " times.");
          {code}
          You either return something or print something to the console. Don't do both. Also, see how the end bracket and semicolon are red. That means you have made an error with your formatting.
          
          I'd say you would need nested loops. Outer loop to iterate over your list of Strings. Inner loop to iterate over each string to see how many times the substring exists in that String.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
          • 2. Re: Compare substrings in strings
            807600
            You should use .equals to compare strings, not ==.

            And you probably don't even need to use .equals either. Here's a hint: you probably want to use String.indexOf
            • 3. Re: Compare substrings in strings
              807600
              string.indexOf

              I was thinking of using the string.charAt

              basically what I need to do is step into each index of the arrayList and go through the string in that index and look for a substring

              so instead of what I had written

              I could write it as this <casting the arrayList to a string>

              for (int i = 0; i <= listofStrings.size(); i ++){
              for(int j = 0; i < j; j++){
              if (listofString.get[j].indexOf[j] .equals keyword.indexOf[j] && listofString.get[j].lastIndexOf[j] .equals keyword.lastIndexOf[j])
              {
              counter += 1;

              }

              }

              }
              • 4. Re: Compare substrings in strings
                807600
                String text = "abcdef";
                String keyword = "bcd";
                if(text.indexOf(keyword) >= 0) {
                    System.out.println(keyword + " exists in " + text);
                }
                • 5. Re: Compare substrings in strings
                  807600
                  thanks

                  curious if this makes sense
                   public int keywordCount(ArrayList listOfStrings, String keyword) {
                       int counter = 0;
                       for (int i = 0; i < arrayList.size(); i ++){
                  
                       String s = (string)arrayList.get;

                       while(s.indexOf(keyword) != -1){
                       counter ++
                       index += keyword.length();
                            }

                  }

                  return counter;

                  }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
                  • 6. Re: Compare substrings in strings
                    807600
                    teknolife wrote:
                    curious if this makes sense
                    Well, what happens when you try to compile it? That's a good first step to seeing whether it makes sense or not.

                    edit Besides about 7 syntax errors, your general logic seem to be in the right place. You need to declare your "index" variable though, and use it in your indexOf() method.

                    Also, something to think about. In the string
                    "aaaa"
                    How many times does the keyword
                    "aa"
                    appear, 2, or 3? If you continue counting at the end of the last found instance, you may miss some.

                    Edited by: endasil on Nov 18, 2007 10:36 PM
                    • 7. Re: Compare substrings in strings
                      807600
                      i think for your while loop:
                      while(s.indexOf(keyword) ! = -1)
                      counter++
                      
                      
                      //removes the string up to the keyword , and also the keyword, creating a new string from that could possibly have another instance of the keyword, but not the original instance.
                      
                      keyword = keyword.substring((s.indexOf(keyword)) + keyword.length());    
                      I'm not the most experienced programmer for sure, but give it a try, I think it might work.
                      • 8. Re: Compare substrings in strings
                        807600
                        tim.raptorrunner wrote:
                        keyword = keyword.substring((s.indexOf(keyword)) + keyword.length());
                        That's not a bad attempt, and may actually work if you changed it to:
                        s = s.substring((s.indexOf(keyword)) + keyword.length());
                        But I think it's overkill (too much string manipulation) give that there's an overloaded indexOf method that takes a String and a starting index:
                        int index = 0;
                        int counter = 0;
                        while ( (index = s.indexOf(keyword, index)) > 0 ) { //find and record the next occurance of keyword
                           counter++; //we've found another occurance
                           index++; //move index one past the last occurance
                        } 
                        • 9. Re: Compare substrings in strings
                          807600
                          Heh, thats what I meant to write haha.