• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculatin number of words repeating in one sentence

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the String.split method is available in 1.4
 
Martin Lerchster
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i�m sleeping lol
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" ?
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How abt this one from JGuru??




Hope this helps Sheetal...

Cheers,
Vijay
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic