More Java Than WebObjects
More Java Than WebObjects
- Subject: More Java Than WebObjects
- From: "Jonathan Fleming" <email@hidden>
- Date: Fri, 11 Jun 2004 12:17:02 +0100
What's the best way to write code that will count the spaces between each
word but not prases that are sourounded by double quotation marks eg. there
are 2 phrases in the example below making up a total of 6 words (6 spaces),
but how can avoid counting the spaces in the phrases?:
walls, plumbing, "painting & decorating", decorating, "concrete mixing
truck", concreting, bricklayers
This is the code I have so far:
public static String wordCounter(String s, int numOfWords){
int wordCounter=0;
int numToReduce=0;
for (int index=0;index<s.length();index++) {
char c = s.charAt(index);
if (Character.isWhitespace(c)) {wordCounter++;} // Counts the
spaces.
}
if (wordCounter > numOfWords) {
numToReduce=wordCounter - numOfWords;
throw new NSValidation.ValidationException
("You are only allowed to enter "+numOfWords+" words, but you
have entered "+wordCounter+". <br />"+
"You'll need to delete any "+numToReduce+" of these words.");
}
return s;
}
_________________________________________________________________
Want to block unwanted pop-ups? Download the free MSN Toolbar now!
http://toolbar.msn.co.uk/
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.