Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Junky

Thread: Count Words

  1. #1
    jjjkkk is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Count Words

    Write a program to enter text string, then count the number of times each word occurs in that string and print to the screen in the format: word - number of words

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    We don't write code for you here. Make an effort and we will help(hint: a Map<String, Integer> will be very helpful).

  3. #3
    jjjkkk is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    This 's my code. but it 's not right.
    I want output:
    example:
    Enter string: I am a student a student not good.
    I: 1
    am: 1
    a: 2
    Student: 2
    not: 1
    good: 1
    Java Code:
    package lab7;
    
    
    import java.io.*;
    
    public class  Main{
      private static void linecount(String fName, BufferedReader in)
      throws IOException{
      long numChar = 0;
      long numLine=0;
      long numWords = 0;
      String line;
      do{
      line = in.readLine();
      if (line != null){
      numChar += line.length();
      numWords += wordcount(line);
      numLine++;
      }
      }while(line != null);
      System.out.println("File Name: " + fName);
      System.out.println("Number of characters: " + numChar);
      System.out.println("Number of words: " + numWords);
      System.out.println("Number of Lines: " + numLine);
      }
      private static void linecount(String fileName){
      BufferedReader in = null;
      try{
      FileReader fileReader = new FileReader(fileName);
      in = new BufferedReader(fileReader);
      linecount(fileName,in);
      }
      catch(IOException e){
      e.printStackTrace();
      }
      }
      private static long wordcount(String line){
      long numWords = 0;
      int index = 0;
      boolean prevWhiteSpace = true;
      while(index < line.length()){
      char c = line.charAt(index++);
      boolean currWhiteSpace = Character.isWhitespace(c);
      if(prevWhiteSpace && !currWhiteSpace){
      numWords++;
      }
      prevWhiteSpace = currWhiteSpace;
      }
      return numWords;
      }
      public static void main(String[] args){
        long numChar = 0;
      long numLine=0;
      String line;
      try{
      if (args.length == 0)
      {
      BufferedReader in =
      new BufferedReader(new InputStreamReader(System.in));
      line = in.readLine();
      numChar = line.length();
      if (numChar != 0){
      numLine=1;
      }
      System.out.println("Number of characters: " + numChar);
      System.out.println("Number of words: " + wordcount(line));
      System.out.println("Number of lines: " + numLine);
      }else{
      for(int i = 0; i < args.length; i++){
      linecount(args[i]);
      }
      }
      }
      catch(IOException e){
      e.printStackTrace();
      }
      }
    }

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I don't see how your code is supposed to do anything to what you asked. This doesn't keep track of what words have been used at all...
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    count the number of times each word occurs in that string
    What kind of techniques and/or classes do you know how to use to be able to remember the counts?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by jjjkkk View Post
    you 're stupid more than me. =))
    If you think a comment like that will encourage people to help you then clearly you are the stupid one!

    Ignore++
    sunde887 likes this.

  8. #8
    acmohan is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    Use as sunde887 suggested Map<String, Integer> .It s the best and easiest way to do it. Try it out.

  9. #9
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    Hi,

    1) Counts the No of Words in a String
    2) Place each of those words in a buffer (means any Array, List r some Storage)
    3) Iterate through that Buffer Starting with the first word
    4) Count the occurrence of the word

    just think like a student solving a problem ......... u can easily build upon the logic

Similar Threads

  1. Read text file and count words.
    By chdn202002 in forum New To Java
    Replies: 5
    Last Post: 07-17-2011, 02:44 PM
  2. Help: Num to words
    By MyOnlineChurva in forum New To Java
    Replies: 16
    Last Post: 01-08-2010, 01:41 AM
  3. Replies: 8
    Last Post: 02-04-2009, 08:55 PM
  4. Replies: 5
    Last Post: 11-22-2008, 01:34 PM
  5. help w words
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 11-21-2007, 06:15 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
  •