Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Llist of words and frequency of each word

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Llist of words and frequency of each word

    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

    output:
    Enter string: I love you and you love who I hate you
    I: 2
    love: 2
    you: 3
    and:1
    who:1
    hate:1


  2. #2
    Junior Member Mrc0d3r's Avatar
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Llist of words and frequency of each word

    You can't expect us to do your homework. Think of an approach and get started with it.Then we can address your queries.

  3. The Following User Says Thank You to Mrc0d3r For This Useful Post:

    jkkj (July 17th, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Llist of words and frequency of each word

    ok. thanks
    It's my code but it 's not right. Can you have me?

    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();
      }
      }
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,252
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Llist of words and frequency of each word

    Does your code compile? If not post the errors.
    Does your code execute? If not post the errors.
    What does the program output?
    Can you show the output and describe what is wrong with it?

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,252
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Llist of words and frequency of each word

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/46556-count-words.html#post222928

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  7. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Llist of words and frequency of each word

    Quote Originally Posted by Norm View Post
    Does your code compile? If not post the errors.
    Does your code execute? If not post the errors.
    What does the program output?
    Can you show the output and describe what is wrong with it?
    I compiled this. It compiles without a problem.

    Output is:
    hello mum
    Number of characters: 9
    Number of words: 2
    Number of lines: 1
    Not the output I was expecting. Did you write this code jkkj?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  8. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Llist of words and frequency of each word

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.LinkedHashMap;

    public class HashMapEx {

    public static void main(String[] args) {
    // Creating new HashMap objects
    // keys are String, values are Integer
    LinkedHashMap<String, Integer> wordcount = new LinkedHashMap<String, Integer>();

    try {

    BufferedReader in = new BufferedReader(new FileReader("c:\\test\\kashif.txt"));
    // string buffer for file reading
    String str;

    // reading line by line from file
    while ((str = in.readLine()) != null) {
    str = str.toLowerCase(); // convert to lower case

    // starting index, we'll use this to copy words from string
    int idx1 = -1;
    // process each characters
    for (int i = 0; i < str.length(); i++) {
    // trigger condition if current character is not letter
    // or it is the end of line
    if ((!Character.isLetter(str.charAt(i))) || (i + 1 == str.length())) {
    // do nothing if previous character was also non-letter
    if (i - idx1 > 1) {
    // copy word from input string buffer to new variable
    // from previous non-letter symbol
    // to current symbol which is also non-letter

    // if this is a letter(than it is last character in the line
    // and we should copy it to word)
    if (Character.isLetter(str.charAt(i)))
    i++;

    // copying...
    String word = str.substring(idx1 + 1, i);

    // Check if word is in HashMap
    if (wordcount.containsKey(word)) {
    // get number of occurrences for this word
    // increment it
    // and put back again
    wordcount.put(word, wordcount.get(word) + 1);
    } else {
    // this is first time we see this word, set value '1'
    wordcount.put(word, 1);
    }
    }

    // remember current position as last non-letter symbol
    idx1 = i;
    }
    }
    }
    // Close buffered reader
    in.close();
    } catch (Exception e) {
    // If something unexpected happened
    // print exception information and quit
    e.printStackTrace();
    System.exit(1);
    }

    // This code sorts outputs HashMap sorting it by values

    // First we're getting values array
    ArrayList<Integer> values = new ArrayList<Integer>();
    values.addAll(wordcount.values());
    // and sorting it (in reverse order)
    Collections.sort(values, Collections.reverseOrder());

    int last_i = -1;
    // Now, for each value
    for (Integer i : values) {
    if (last_i == i) // without dublicates
    continue;
    last_i = i;
    // we print all hash keys
    for (String s : wordcount.keySet()) {
    if (wordcount.get(s) == i) // which have this value
    System.out.println(s + ":" + i);
    }

    // pretty inefficient, but works
    }
    }
    }

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,252
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Llist of words and frequency of each word

    Please wrap you code in code tags to preserve its formatting. See: BB Code List - Java Programming Forums
    or Go Advanced and use the #icon

Similar Threads

  1. numbers to words using do-while & if-else & JOptionPane
    By mikkko in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 15th, 2011, 06:28 AM
  2. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 08:52 AM
  3. Finding frequency and probability of characters in a string
    By Aberforth in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 03:02 AM
  4. Help: Num to words
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2010, 06:55 PM
  5. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 10:23 AM