Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Calculatin number of words repeating in one sentence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculatin number of words repeating in one sentence" Watch "Calculatin number of words repeating in one sentence" New topic
Author

Calculatin number of words repeating in one sentence

Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
Hi,

I want to know that how to calculate the number of words which are repeating in one sentence.

Let a sentence as an example :

My first name is Sheetal, My last name is Kaul.

Here in this sentence, My word is repeating for two time, so i want the ans as My -- 2times ans so on.

So plz tell me how to do the program for this, & also want to know about the alphabets, like a is repeating for how many times.

Thanxs
Sheetal
Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
Maybe some might beat me up for such a solution,
but it's the simplest way I know to solve your problem:



Best regards ...
- Thomas -
Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
Hi,

That sentence was just an Example, i want to calculate the number of words which are repeating in the sentence, where sentence is any sentence that user enter.

Plz explain me how to do that. I used your program. bt not getting how to pass the array instead of p there.

i did like this :

--------------------------------------------------------------------

import java.util.*;
import java.io.*;
import java.*;


public class CountWords {

public static void main(String[] args) throws IOException
{

InputStreamReader data = new InputStreamReader(System.in);
BufferedReader strData = new BufferedReader(data);

System.out.print("Enter Data :: ");

String strMainData = " ";

strMainData = strData.readLine();

System.out.print("Entered Data :: " + strMainData);

String strParts[];
strParts = strMainData.split(" ");

//String s = "My first name is sheetal, My last name is kaul.";
//String p = "My";
for(int i = 0; i < strParts.length; i++)
{
System.out.println("'" + strParts[i] + "' occures " + CountWords.count(strMainData, strParts[i]) + " times in '" + strMainData + "'");
}


}

public static int count(String s, String p[])
{
String sTemp = s;
int counter = 0;
while (sTemp.length() > 0)
{
for(int j = 0; j < sTemp.length; j++) {

int index = sTemp.indexOf(pattern[j]);
if (index == -1)
break;
sTemp = sTemp.substring(index + pattern[j].length(), sTemp.length());
counter++;
}
}
return counter;
}
}

--------------------------------------------------------------------
but it is giving error. Plz explain me how to do thia program.


Thanxs
Sheetal
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
Hi Sheetal

You can use java.utilStringTokenizer class to get strings after specifying the delimiter character between each string. and then compare each string using String.equals().

Here is example( not exact one but just for using StringTokenizer()

http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
Martin Lerchster
Greenhorn

Joined: Nov 29, 2004
Posts: 10
String strMainData = " ";
String strParts[];
strParts = strMainData.split(" ");

String don�t have method split


+ test.count(strMainData, strParts[i])

public static int count(String s, String p[])

you are trying to give a String as Parameter to a String array!

you can call test.count(strMainData, strParts)
or change the method argument to public static int count(String s, string p)


sTemp.length should be sTemp.length()


what editor do you use?
[ December 30, 2004: Message edited by: Martin Lerchster ]
Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
Hi,

-----------------------------
String don�t have method split
-----------------------------

String have split method. i'm using that method only. & my Program is working, simply i have changed my parameter to String which i was passing as String array.

& then no need to use the For loop in count method.

i'm using JCreator.

Thanxs for all your Help.

Sheetal
Marco Davids II
Greenhorn

Joined: Nov 02, 2004
Posts: 24
the String.split method is available in 1.4
Martin Lerchster
Greenhorn

Joined: Nov 29, 2004
Posts: 10
i think i�m sleeping lol
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd tackle this as several problems. Small steps are always easier and more fun - you get more success points on the way.

get a word from the line

keep a counter per word

report the results

StringTokenizer is a good way to get words. If you're in a later JDK String split or regular expressions or Scanner might be better but trickier. Watch out for punctuation. A word at the end of a line will have a period attached. Same problem for commas, quotes, semi-colons, colons, exclamation points, question marks, etc. I'd look for some way to remove all the non-letters from the sentence before I start looking for words.

Now how to keep a counter for each word. Are you familiar with the Map interface? You could use the word as a key and store the number of occurrences for each word. If you're not in JDK 5 this has a slight challenge of storing your count as an object, say Integer instead of int.

Finally, to report your results there are ways to go through all the keys in the map and write something about each one.

Why not try to solve the first problem - getting words. See what kind of code you come up with. Test it with tricky punctuation to see if it cleanly selects each word.

My dog, Bob, (my constant companion) is named after my friend Bob.

BTW: What's your rule on ignore case? Does "My" match "my" ?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Vijay Vaddem
Ranch Hand

Joined: Feb 13, 2004
Posts: 243
How abt this one from JGuru??




Hope this helps Sheetal...

Cheers,
Vijay
 
Consider Paul's rocket mass heater.
 
subject: Calculatin number of words repeating in one sentence
 
Similar Threads
Guidance needed
finding duplicate characters in a String
how to take a series of string from command line?
Need a solution to read and store data from a file
Question about javadoc