StringTokenizer사용시에
";;;;a;a;" 를 token 해보면 공백문자가 무시되는 단점때문에 만들어 보았습니다.
위의 스트링을 countTokens()해보면 2 라고 나오고 공백들이 무시되잖아요.
제대로 7이라고 반환하도록 만들어 보았습니다.
소스는 jboss 의 샘플들 중에서 String[]을 반환하는 tokenizerWithBlanks라는 메쏘드를
이용해서 StringTokenizer와 유사한 인터페이스의 클래스를 만들어 보았는데요,
원본은 누가 만들었었는지는 알수가 없네요.
import java.util.ArrayList;
import java.util.NoSuchElementException;
/**
* This simple class has one method that tokenizes a string, but allows tokens
* to be blanks. This is in contrast to the java.util.StringTokenizer class
* that assumes that multiple delimiters are a single delimiter. This class is
* designed to tokenize the output of spreadsheets, where not all cells have
* entries
*/
/**
* @(#) Tokenizer.java
* @version 1.0
* @author Edited by Woo-Chan Cho, number501@yahoo.com.
*/
class Tokenizer
{
protected Object[] tokened_array=null;
protected int token_count=0;
/**
* Constructor
* @param string -a string to be parsed.
* @param delimi -the delimiters.
*/
public Tokenizer(String string, String delim)
{
tokenizerWithBlanks(string, delim);
}
/**
* Calculates the number of times that this tokenizer's
* nextToken method can be called
* @return tokend string
*/
public int countTokens()
{
return tokened_array.length-token_count;
}
/**
* Tests if there are more tokens available from this tokenizer's string.
* If this method returns true, then a subsequent call to nextToken with
* no argument will successfully return a token.
* @return true if and only if there is at least one token in the string after
* the current position; false otherwise.
*/
public boolean hasMoreElements()
{
return hasMoreTokens();
}
/**
* Tests if there are more tokens available from this tokenizer's string.
* If this method returns true, then a subsequent call to nextToken with
* no argument will successfully return a token.
* @return true if and only if there is at least one token in the string after
* the current position; false otherwise.
*/
public boolean hasMoreTokens()
{
if(tokened_array.length>token_count)
return true;
return false;
}
/**
* Returns the next token from this string tokenizer.
*
* @return tokend string
* @exception NoSUchElementException
* if there are no more tokens available from this tokenizer's string.
*/
public String nextToken() throws NoSuchElementException
{
if(token_count>=tokened_array.length)
throw new NoSuchElementException("Hey, I have no more element.");
token_count++;
return tokened_array[token_count-1].toString();
}
protected void tokenizerWithBlanks (String input, String delimiter)
{
ArrayList array = new ArrayList();
String token;
int pos;
do{
pos = input.indexOf(delimiter);
if (pos >= 0)
{
token = input.substring(0, pos);
input = input.substring(pos + 1);
}
else
{
token = input;
input = "";
}
array.add(token);
}while (pos >= 0);
tokened_array = array.toArray();
}
}
";;;;a;a;" 를 token 해보면 공백문자가 무시되는 단점때문에 만들어 보았습니다.
위의 스트링을 countTokens()해보면 2 라고 나오고 공백들이 무시되잖아요.
제대로 7이라고 반환하도록 만들어 보았습니다.
소스는 jboss 의 샘플들 중에서 String[]을 반환하는 tokenizerWithBlanks라는 메쏘드를
이용해서 StringTokenizer와 유사한 인터페이스의 클래스를 만들어 보았는데요,
원본은 누가 만들었었는지는 알수가 없네요.
import java.util.ArrayList;
import java.util.NoSuchElementException;
/**
* This simple class has one method that tokenizes a string, but allows tokens
* to be blanks. This is in contrast to the java.util.StringTokenizer class
* that assumes that multiple delimiters are a single delimiter. This class is
* designed to tokenize the output of spreadsheets, where not all cells have
* entries
*/
/**
* @(#) Tokenizer.java
* @version 1.0
* @author Edited by Woo-Chan Cho, number501@yahoo.com.
*/
class Tokenizer
{
protected Object[] tokened_array=null;
protected int token_count=0;
/**
* Constructor
* @param string -a string to be parsed.
* @param delimi -the delimiters.
*/
public Tokenizer(String string, String delim)
{
tokenizerWithBlanks(string, delim);
}
/**
* Calculates the number of times that this tokenizer's
* nextToken method can be called
* @return tokend string
*/
public int countTokens()
{
return tokened_array.length-token_count;
}
/**
* Tests if there are more tokens available from this tokenizer's string.
* If this method returns true, then a subsequent call to nextToken with
* no argument will successfully return a token.
* @return true if and only if there is at least one token in the string after
* the current position; false otherwise.
*/
public boolean hasMoreElements()
{
return hasMoreTokens();
}
/**
* Tests if there are more tokens available from this tokenizer's string.
* If this method returns true, then a subsequent call to nextToken with
* no argument will successfully return a token.
* @return true if and only if there is at least one token in the string after
* the current position; false otherwise.
*/
public boolean hasMoreTokens()
{
if(tokened_array.length>token_count)
return true;
return false;
}
/**
* Returns the next token from this string tokenizer.
*
* @return tokend string
* @exception NoSUchElementException
* if there are no more tokens available from this tokenizer's string.
*/
public String nextToken() throws NoSuchElementException
{
if(token_count>=tokened_array.length)
throw new NoSuchElementException("Hey, I have no more element.");
token_count++;
return tokened_array[token_count-1].toString();
}
protected void tokenizerWithBlanks (String input, String delimiter)
{
ArrayList array = new ArrayList();
String token;
int pos;
do{
pos = input.indexOf(delimiter);
if (pos >= 0)
{
token = input.substring(0, pos);
input = input.substring(pos + 1);
}
else
{
token = input;
input = "";
}
array.add(token);
}while (pos >= 0);
tokened_array = array.toArray();
}
}