Results 1 to 5 of 5
  1. #1
    noobinoo is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default please help with counting specific word in a sentence

    how do i count a specific word in a sentence. example:
    "i have a dog"
    the word "dog" occurred 1 time

    here is the code i that i develop so far

    Java Code:
    public class count{
     public static void main(String[] args){
        String sentence = "this is a book this is a cat this is a dog";
         char []abc= new char[26];
          int []times=new int [26];
           for(int i=0; i<abc.length;i++)
             abc[i]=(char) (i+97);
              for(int i=0; i<sentence.length();i++){
               char c=sentence.charAt(i);
               for (int j=0;j<abc.length;j++)
                if (c==abc[j]) times[j]++;
           }  
       for(int i=0;i<times.length;i++)
          System.out.println(abc[i]+" was used "+times[i]+" times");
        }
    
     }
    i thought that if i count each letter first then i might be able to have some idea on how to count a specific word. please help me thanks

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Suppose that you split the sentence into an array of words ...

  3. #3
    noobinoo is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    thanks,i'll try to do that

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The String.split method comes to mind here for the splitting into array part.

  5. #5
    noobinoo is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

Similar Threads

  1. enter a string sentence
    By amorosa19 in forum New To Java
    Replies: 13
    Last Post: 09-06-2013, 10:34 AM
  2. Counting specific word from a file
    By jaq in forum New To Java
    Replies: 2
    Last Post: 12-02-2009, 06:12 PM
  3. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  4. [SOLVED] Help on Word and Character counting in java
    By Alistair in forum New To Java
    Replies: 2
    Last Post: 05-15-2008, 03:48 AM
  5. How to extract info from a sentence
    By luisarca in forum XML
    Replies: 1
    Last Post: 06-07-2007, 05:43 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
  •