|
-
April 2nd, 2012, 08:14 PM
#1
counting words in a string array
Hi, I'm new to this site, so please be gentle if I don't do something right.
I'm having a bit of an issue with this code:
Code:
public void actionPerformed(ActionEvent e)
{
String textContents;
textContents = textEntryField.toString();
String[] words = textContents.split("\\s");
int wordCount = 0;
//count number of words
for(int i = 0; i < words.length; i++)
{
if(!words[i].equals("\\s") && !words[i].equals(""))
wordCount++;
}
//set jtextfield to value
wordCountField.setText(Integer.toString(wordCount));
}
What I'm trying to do is split a String, by white-space, and fill an array with it. The string is in a JTextArea.
When a button is clicked, it is supposed to pull the String data from the JTextArea (textEntryField), pass it into a String, split that String by white-space, and throw it into the array. For each entry/word in the array, the counter (wordCount) is supposed to increase by 1. However, no matter what I enter into the JTextArea, I always get a result of 1 and only 1.
Is there anything you guys see that's potentially causing me issues? Thank you in advance for your help.
Side note: There's only one button as of now, hence why there's no testing to see what button caused the actionPerformed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|