• 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
Fwd: More Java Than WebObjects - [The Code]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fwd: More Java Than WebObjects - [The Code]


  • Subject: Fwd: More Java Than WebObjects - [The Code]
  • From: Daniel Mejia <email@hidden>
  • Date: Sun, 13 Jun 2004 18:26:32 -0500

Jonathan,

I think that you could have problems if the string comes with many spaces between words and with no spaces between word and the quotes.

Here is a pice of code that solve that cases.

/*
 * Ejemplo para contar palabras en un String y parrafos encerrados en
 * comillas.
 */

import java.io.StreamTokenizer;
import java.io.CharArrayReader;

public class CountWords {
int quotes = 0;
static char QUOTE = '\"';
String str;

    public CountWords(String s){
	str = s;
    }

    /*
     * count (") characters. If odd number, do nothing.
     */
    public int countQuotes(){
	int idx = 0;
	int i = 0;
	while ( (idx = str.indexOf(QUOTE, idx)) != -1){
	    i++;
	    idx++;
	}
	System.out.println("Valor de countQuotes: " + i);
	return i;
    }

// count paragrphs adn words , defining paragraph as any number of chaaracters
// surrounded by quotes.
public int count() {
int count = 0;
CharArrayReader cr = new CharArrayReader(str.toCharArray());
StreamTokenizer st = new StreamTokenizer(cr);
st.quoteChar('\"');
String nxt = "";


	try {
	    while (st.nextToken() != StreamTokenizer.TT_EOF) {
		nxt = st.toString();
		count++;
	    }
	} catch(Exception e){
	    e.printStackTrace();
	}
	return count;
    }

    public static void main(String args[]){
    String test = new String ("Esto \"es \"solo \"    una prueba");
    CountWords cw = new CountWords(test);
	if ((cw.countQuotes() % 2) == 0)
	    System.out.println("Number of words: " + cw.count());
	else
	    System.out.println("Number of quotes invalid!!!");

    }
}

Saludos,

Daniel.
_______________________________________________
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: [OT] WebObjects, Point of Sale, and Credit Card scanners
  • Next by Date: apache mod_gzip and WebObjects?
  • Previous by thread: Re: More Java Than WebObjects - [The Code]
  • Next by thread: Re: More Java Than WebObjects - [The Code]
  • Index(es):
    • Date
    • Thread