• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: More Java Than WebObjects - [The Code]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: More Java Than WebObjects - [The Code]


  • Subject: Re: More Java Than WebObjects - [The Code]
  • From: "Jonathan Fleming" <email@hidden>
  • Date: Sat, 12 Jun 2004 19:02:25 +0100

Hi guys I finished that piece of code I've been trying to write and here it is... if anyone can make it any better, tidy it up and all that stuff, please mail me a copy.
I hope it helps some of you and thanks to all of you whom responded and promted to help me out here.


// Copyright ) Jonathan Fleming Sat June 12 18:18:47 GMT 2004
/*
This little class will enable you to validate the number of words entered for a meta description text field or a meta keywords text field. It will validate text in double quotes (phrases) as one word else if a quote is unclosed it will throw an exception of count the words in the unclosed quote as single words.
I've left in the NSLog methods in so you can see what's going on, but you can take these out or uncomment them as you wish.


The "if (inQuote)" statement checks that the user has not left an open quotation, if they have it throws and exception. You can if you want remove the "if (inQuote)" statement, but if the "if (inQuote)" statement is kept in this class, then the "if (countersTotal > numOfWords)" statement will never be called or throw an exception, however, if you remove the "if (inQuote)" statement the "if (countersTotal > numOfWords)" will read all the words that should have been in the unclosed phrase ensuring that the user knows they are inadvertently adding more words than they think they are.

Thanks to Michael Engelhart <email@hidden> and Adam Ramadan <email@hidden> for your pointers and all the others who offered help and opinions.
If anyone can make this class any better I'd love to get hold of a copy, please mail it to me at email@hidden
Thanks
Jonathan :^)
*/


public class MetaSearchWordCounter  {

public static String wordCounter(String s, int numOfWords){
int wordCounter=0;
int phraseWordCounter=0; // used to add to the wordCounter if a quote is not closed
int countersTotal=0;
int numToReduce=0;
char lastChar=' ';
boolean inQuote=false;
for (int index=0;index<s.length();index++) {
char c = s.charAt(index);
if ( c != '\"' && !inQuote) {
if (Character.isWhitespace(c)) {wordCounter++; phraseWordCounter=0;} // Counts the spaces.
}
else if ( c == '\"' && !inQuote ) {
inQuote = true;
}
else if ( c == '\"' && inQuote ) {
inQuote = false; phraseWordCounter=0;
}
else if (inQuote) {
if (Character.isWhitespace(c)) {phraseWordCounter++;}
}
lastChar = c;
NSLog.out.appendln("===\r"+
" c = " + c + "\r"+
" inQuote: " + inQuote + "\r");
}
if (!Character.isWhitespace(lastChar))wordCounter = wordCounter+1; // adds a space to balance the wordCounter by 1 with actual words typed
if (s.length()>0 && String.valueOf(lastChar).trim()!=null && wordCounter == 0) wordCounter = 1; // used if only one word is entered
countersTotal = wordCounter+phraseWordCounter;
NSLog.out.appendln(" numOfWords = " + numOfWords + "\r"+
" wordCounter = " + wordCounter + "\r"+
" phraseWordCounter = " + phraseWordCounter + "\r"+
" added Counters Total = " + countersTotal + "\r"+
" inQuote = " + inQuote + "\r");
if (inQuote) {
throw new NSValidation.ValidationException
("You have unclosed quotes in your text, please be sure to close all double quoted phrases. <br />"+
"Your text was:<br /> "+
s);
}
if (countersTotal > 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.");
}
else return s;
}


}


From: Adam Ramadan <email@hidden>
To: Jonathan Fleming <email@hidden>
Subject: Re: More Java Than WebObjects
Date: Fri, 11 Jun 2004 10:54:35 -0400

A simple approach would be to add a boolean variable, inQuote, and use that to track whether or not you're within a quoted phrase. So asy ou are iterating through the characters and reach a quote you set inQuote <- !inQuote, and when you reach a whitespace character you can use the value of inQuote to decide whether to count the word.

For anything more complicated you might want to look at regular expressions.

Adam

Jonathan Fleming wrote:

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.



_________________________________________________________________
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.



  • Prev by Date: Re: More Java Than WebObjects
  • Next by Date: [OT] WebObjects, Point of Sale, and Credit Card scanners
  • Previous by thread: Re: Binding in WObrowser
  • Next by thread: Fwd: More Java Than WebObjects - [The Code]
  • Index(es):
    • Date
    • Thread