hava

hava

write a program to count the no of repeated words in a sentence by using a return statement?

View Answers

February 14, 2011 at 4:28 PM

Java Count Word Occurrences

import java.util.*;
class  CountWordOccurences{
    public static TreeMap count(String str){
        TreeMap<String, Integer> map = new TreeMap<String, Integer>();
        str = str.toLowerCase();
    StringTokenizer stk = new StringTokenizer(str, " \t\n\r\f.,;:!?'");
    while (stk.hasMoreTokens()) {
    String word = stk.nextToken();
    Integer frequency = map.get(word);
    if (frequency == null) {
    frequency = 0;
    }
    map.put(word, frequency + 1);
    }
    return map;
    }

    public static void main(String[] args){
    String st="Where there is will, there is a way";
    TreeMap<String, Integer> map = count(st);
    Set set = map.entrySet();
    Iterator i = set.iterator();
    while(i.hasNext()) {
    Map.Entry m = (Map.Entry)i.next();
    System.out.print(m.getKey() + "\t:\t ");
    System.out.println(m.getValue());
     } 
    }
}









Related Tutorials/Questions & Answers:
hava
Hibernate nullpointer exception - Hibernate
eclipse I hava created new javaproject. 2.I hava copied Contact.java. and I placed it in a src folder. 3.I hava copied FirstExample.java and placed
Advertisements
print word with max vowels
print word with max vowels  if suppose i hava entered a sentence and in that sentence i have to print a word which has maximum vowels in it.... i have tried it but i am not getting it...please help
please help me.
please help me.  I have three table in mysql,and i hava create a excel sheet and add this sheet.but my question is in every sheet i can display one one table result.how can i do
Combo Box - Struts
Combo Box  I hava acombo box cnnected to my database via html collections. When selecting an option, i want to display specific parts (sectons) on my web page....am trying to use logic:present and logic:empty but its not working
Upload Excel into Database Table Using SERVLET - JSP-Servlet
Upload Excel into Database Table Using SERVLET  Hi i hava sitution that upload .xls file into database table which has same coloumn names as in excel sheet. Example: Excel sheet has coloumn Headers as NAME,ID,SAL. Coloumns
j2me problem - SQL
j2me problem  Hi i hava a j2me (mobile application) having 3 forms each one having 4 fields(text box and dropdown ).i want to get the data from thouse fields, and store into sql data base in my local pc. How intract
Hibernate error - Hibernate
Hibernate error  I hava following error while running hibernate example in eclipse. I placed hibernate.cfg.xml and contact.hbm.xml in bin/roseindia/tutorial/hibernate. But I have following error. log4j:WARN No appenders
jsf - Java Server Faces Questions
in blue colour in italic style,then tell me the code how to get it.I hava one
JTextBox - Java Beginners
JTextBox   i hava 3 textboxes. 1. Income 2.Expense 3.Balance=fix 500 in first. If i enter income amount then the balance amount increment automatically.(ie) i am enter 200 in first textbox then the balance textbox value
error of HTTP Status 404 while running servlet on apache tomcat server
error of HTTP Status 404 while running servlet on apache tomcat server  i have followed the steps given here.Also hava put servlet-api in lib folder and have set classpath properly. on typing "http://localhost:8080" i do get
Basic problem but very urgent - JSP-Servlet
Basic problem but very urgent  Respected Sir/Madam, I am R.Ragavendran.. Thanks for your superb reply. I got the link you sent. But I find a simple problem which i hava tried my level best to solve.In the output, the text box
java - JSP-Servlet
java  Hi i want to create one jsp page that that will have onebutton when click that button start the main thread. i have another button when click that button start the child thread. i hava another button when click
java default constructor
java default constructor  suppose i hava a class: public class Student{ private int rollNo; private String name; } then what would be the default constructor: 1) public Student(){ private int rollNo = 0; private
Java Break keyword
); JOptionPane.showMessageDialog(null, "Congratulations\n good buy \t&\n\t hava... \t&\n\t hava a nice day", " \to",1); } else if ((choice.equals(rt
Program Very Urgent.. - JSP-Servlet
Program Very Urgent..  Respected Sir/Madam, I am R.Ragavendran.. Thanks for your superb reply. I got the link you sent. But I find a simple problem which i hava tried my level best to solve.In the output, the text box
jsp - Java Interview Questions
. If you hava a jsp file while you acccesing it will be TRANSLATED into servlet
Convert InputStream to BufferedReader
the byte form data into character form we hava to wrap it 
Simple problem Very Urgent - JSP-Servlet
Simple problem Very Urgent  Respected SDir/Madam, I am R.ragavendran.. Thanks for your superb reply. I got the coding. But I find a simple problem which i hava tried my level best to solve. A pop up window is opening
I am getting Undefined in Text Box Very Urgent - JSP-Servlet
I am getting Undefined in Text Box Very Urgent  Respected Sir/Madam, I am R.Ragavendran.. Thanks for your superb reply. I got the coding. But I find a simple problem which i hava tried my level best to solve. A pop up window
Search Class - Java Beginners
Search Class  Hi i have created a search class with 3 txtfield for user input to make a search on the database. However i do not know how to forward the output to another form rather than the search class i created. I hava
second selectbox of dynamic
second selectbox of dynamic  hello friends i hava a code like this and could any body help me with editing code and send me code. i am getting city values but i can't location values of city from database.thanks in advance

Ads