Re: CSV Parsing algorithms in Java
in message <8HtvetBvMhTFFwg1@nowhere.nnn>, Jeffrey Spoon
('JeffreySpoon@hotmail.com') wrote:
In message <s7fv14-idn.ln1@gododdin.internal.jasmine.org.uk>, Simon
Brooke <simon@jasmine.org.uk> writes
Thanks to the others who suggested as well, I'll get around to them.
Heavens, writing a CSV parser is trivial. It's simply a case of a
StringTokenizer in a for loop:
Except I wasn't allowed to use String Tokenizer, as I said in the
original post, "I'm not interested in using library functions".
Then write your own; it's a trivial thing to do. Here, in fact, is one I
wrote earlier:
/**
* MIDP does not provide a StringTokenizer. Because this has to be
* compatible with MIDP we'll provide our own. If you have access to a real
* StringTokenizer don't use this one - it is minimal and possibly
* inefficient.
*/
public class StringTokenizer
{
//~ Instance fields -----------------------------------------------
/** the source string, which I tokenize */
private String source = null;
/** the separator character which I split it on */
private char sep = ' ';
/** my current cursor into the strong */
private int cursor = 0;
//~ Constructors --------------------------------------------------
/**
* @param sep the separator which separates tokens in this source
* @param source the source string to separate into tokens
*/
public StringTokenizer( String source, char sep )
{
super( );
this.sep = sep;
this.source = source;
}
//~ Methods -------------------------------------------------------
/**
* @return true if this tokenizer still has more tokens, else false
*/
public boolean hasMoreTokens( )
{
return ( ( source != null ) && ( cursor < source.length( ) ) );
}
/**
* Test harness only - do not use
*
* @param args
*/
public static void main( String[] args )
{
if ( args.length == 2 )
{
StringTokenizer tock =
new StringTokenizer( args[0], args[1].charAt( 0 ) );
System.out.println( "String is: '" + args[0] + "'" );
System.out.println( "Separator is: '" + args[1].charAt( 0 ) + "'" );
for ( int i = 0; tock.hasMoreTokens( ); i++ )
{
System.out.println( Integer.toString( i ) + ": '" +
tock.nextToken( ) + "'" );
}
}
}
/**
* @return the next token from this string tokenizer, or null if there are
* no more.
*/
public synchronized String nextToken( )
{
String result = null;
int end = source.indexOf( sep, cursor );
if ( cursor < source.length( ) )
{
if ( end > -1 )
{
result = source.substring( cursor, end );
cursor = end + 1;
}
else
{
result = source.substring( cursor );
cursor = source.length( );
}
}
return result;
}
}
--
simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
begin 666 this_is_not_a_virus.vbs
There is no virus attached to this post.
end