jung
Posts: 5
Nickname: jung
Registered: Oct, 2003
|
|
Re: a menthod to print a token by token each time you call
|
Posted: Nov 1, 2003 8:18 AM
|
|
Thanks, bluegreen, but i'm too stupid to transfer your words to codes, so i just post my questions and codes, hope you or anyone can help me to work this out. Thanks!
This is my assignment: Write a class that works as a filter for StringTokenizer. Your class should take a word length (>= 0) and a sentence (String) in its constructor and then it should return the next word from the sentence that is at least word length long on each subsequent call to its method nextWord(). When you implement your class you can use StringTokenizer to tokenize the sentence into words And this is my code so far:
public class WordFilter{
String temp;
String sentence1;
int wordLength1;
public WordFilter(String sentence, int wordLength){
sentence1 = sentence;
wordLength1 = wordLength;
}
public String NextWord(){
StringTokenizer st= new StringTokenizer(sentence1);
int number = st.countTokens();
String strArr[] = new String[number];
for( int i=0; st.hasMoreTokens(); i++){
if(strArr.length >= wordLength1)
strArr[i] = st.nextToken(); temp = strArr[i];
}return temp;
}
}
public class WordFilterTest{
public static void main (String[] args) throws Exception {
WordFilter x = new WordFilter("you are really great!",4);
x.NextWord();
System.out.println(x.NextWord());
the problem i have are two: 1.this code do not check token length 2.it can not printout a token properly.
|
|