Simple word count
Hi its me again! Boy do i have a loot of doubts. Anyways, id like to know how I could create a simple wordcount program in java(preferably using BlueJ but java will do).
Im not allowed to use the String tokenizer.. This is the code im using but i get the error: operator == cannot be applied to char java.lang.String
Code:
publicclass WordCount
{
private String str;
privateint w;
publicvoid display(String str)
{
String st=str.trim();
for (int i=0;i<=st.length();i++)
{
if(st.charAt(i)==" ")
w++;
}
}
}
null
[1142 byte] By [
Overkilla] at [2007-11-27 8:18:13]

You're trying to compare a char with a String. You should comparest.charAt(i)with the char ' ' // single quotes
Thankyou! But i have yet another problem. I did what you said and the program works, but no matter what I enter i get an error saying :
StringIndexOutOfBoundsException
String index out of range : x (in java.lang.String)
For example, when i type
"This is a sentence "
x becomes 18.. I know what StringIndexOutOfBounds means, as in it means trying to access a place in a string that doesnt exist, but the loop only runs from 0 to the strings length, so why should it go beyond that..
"This is a sentence"That String has 18 characters, indexed from 0 to 17. In your for loop, you are running from 0 to 18.Keep in mind that the length() method will return the number of characters in the String, not the highest index.
Ohhh thanks! I set the loop to run from 0 to st.length-1 now, but alas! another problem.with "This is a sentence " i get words as 3 not 4 and with "This is bullshit" i get words as 5 not 3.. :S.Thanks in advance.
> Ohhh thanks! I set the loop to run from 0 to
> st.length-1 now, but alas! another problem.
You could have done this also.
for (int i=0; i<st.length(); i++)
> with "This is a sentence " i get words as 3 not 4
> and with "This is bullshit" i get words as 5
> not 3.. :S.
Can you post your current code?
public class ExamQuestion1
{
private String str;
private int w;
public void display(String str)
{
String st=str.trim();
for (int i=0;i<=st.length()-1;i++)
{
if(st.charAt(i)==' ')
w++;
}
System.out.println("Number of words: "+w);
}
}
Can you use String.split()?~Tim
> with "This is a sentence " i get words as 3 not 4Well you're counting the number of spaces, not words. There should be one more word than space.
Instead of comparing to ' ' you might want to use Character.isWhitespace(char).
ExamQuestion1? Is this part of a test for school?
> ExamQuestion1? Is this part of a test for school?Woopsie.
This is a question from the exam i just gave in school and i couldnt crack this question. Hoping for replies why it malfunctions..And we have not learnt about Character.isWhitespace(char).or String.split() so i cannot use those..Message was edited by: Overkill
In your current code your counting the no. of spaces.....not no of words.
if your string is 'This is a String' .it has 3 spaces so the count is 3 not 4.
if your string is 'This is good' .it has 5 spaces so the count is 5 not 3.
since u r using trim().it will remove all leading and trailing spaces from ur input.
After triming the string, u shld append some character deliberatly at the end of the string to recognize the last word. may be u can append a ' ' (space) to the input string after triming it.
EKTa at 2007-7-12 20:03:42 >

Thanks a lot , the word count works pretty much as expected now :)...
I revamped the whole logic, and the code now counts words properly regardless of spaces in between etc.
public class ProperWordCount
{
public void Count(String str)
{
int wordcount=0;
for (int i=0;i<=str.length()-2;i++) //str.length()-2 becuase charAt starts from 0 and length starts from 1(-1), and the second minus because i+1 is also checked, not only i, so using str.length()-1 would give StringOutOfBoundsException(-1)
{
if ((str.charAt(i))!=' ' && str.charAt(i+1)==' ') //Checks if character at i is anything but a space, and character after i is a space, then add 1 word to wordcount
wordcount++;
}
if (str.charAt(str.length()-1)!=' ') //If the last word does not have a space after it, it will not count as word normally. This makes sures that the last word is counted too
wordcount++;
System.out.println("No of words :"+wordcount);
}
}
I know you say you haven't been told about it yet but the split method could be used like this:
private int wordCount(String string){
return string.split(" ").length;
}
Just thought you might like too know ;-)
Thanks bout telling me how to use the split thing, though i wont be using that for wordcounts , since my program works better(counts words even with multiple spaces before/between/after words)
here is an even better version using split, may not be perfect, but I am no regex expert.
public static void main(String[] args) throws FileNotFoundException {
String duh = "This is a weird sentence, with lots of strange punctuation; grammar; and other (not so interesting) stuff!!";
String [] result = duh.split("(\\s|\\p{Punct})+");
for(String str : result)
{
System.out.println(str);
}
System.out.println(result.length + " words");
}
~Tim