| | |
word count
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
I have a method that counts the number of words in a JTextArea. It works pretty good, except for the fact it counts characters that's not letters as words(such as "!@#$" would be a word)...
Here is the code that I have got so far(no erros, compiles and runs fine, just needs to be more specific in what it searches for)
Here is the code that I have got so far(no erros, compiles and runs fine, just needs to be more specific in what it searches for)
Java Syntax (Toggle Plain Text)
public void processWordCount() { String data = textArea2.getText(); Scanner s = new Scanner(data); Pattern p = Pattern.compile(" "); String words = null; int count = 0; while (s.hasNext()) { words = s.next(); count += 1; } JOptionPane.showMessageDialog(null, "Word Count: " + count); }
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
I see what you mean, but actually the code I posted covers that. I tried this:
One
Two
Three
Four
On seperate lines withough any space, and it showed up as four words. I thought it would have the effect you were suggesting.
So do you personally think this would be ok, or would you make it more specific in what it defines as a word?
One
Two
Three
Four
On seperate lines withough any space, and it showed up as four words. I thought it would have the effect you were suggesting.
So do you personally think this would be ok, or would you make it more specific in what it defines as a word?
you could use:
java.util.StringTokenizer
java.util.regex.Pattern
but when u use pattern, make sure that each string token contains at least something to the effect of [a-zA-z0-9], if it does then
count += 1;
java.util.StringTokenizer
java.util.regex.Pattern
but when u use pattern, make sure that each string token contains at least something to the effect of [a-zA-z0-9], if it does then
count += 1;
![]() |
Similar Threads
- word count (C++)
- Word count help. (C++)
- Can you please help me write this word count program in another way (Python)
- I Need Help Writing A Word Count Program In My Python (Python)
- Need Help, basic word count, string manipulation (VB.NET)
- word count in borland c++ ?? (C++)
- I can't implement a word count into my text editor (JAVA) (Java)
Other Threads in the Java Forum
- Previous Thread: Failed to load image into Jpanel
- Next Thread: Chungaroo
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu windows working






